Results 1 to 4 of 4

Thread: Small trouble with preg_replace

  1. #1
    Join Date
    Feb 2010
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Small trouble with preg_replace

    Hey guys!
    I'm working on this little script but I’m novice with PHP and I can not find out solution for this alone, so please will somebody help me.
    Link looks like this:
    <a href="word1-word2-word3">word1-word2-word3</a>
    And I need to be without hyphens like this in part highlighted with red
    <a href="word1-word2-word3">word1 word2 word3</a>

    Here is my script

    <?
    function makeLinksFromWords($text)
    {
    $text = html_entity_decode($text);
    $text = " ".$text;

    $text = preg_replace('/ link:(\S+)/', ' <a href="$1">$1</a>', $text);
    return $text;
    }

    // Live Example
    echo makeLinksFromWords("This is a example for link link:word1-word2-word3");
    ?>


    Would appreciate some help with this one and thank you advance
    Cheers!

  2. #2
    Join Date
    Feb 2010
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    This is Ok :)

    $text = preg_replace('/ link:(\S+)/e', '" <a href="."\\1".">".str_replace("-"," ","\\1")."</a>"', $text);

  3. #3
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    try this...

    PHP Code:
    $text str_replace('-'' '$text); 

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    PHP Code:
    $text=preg_replace('/(<a.+>)(.+)(<\/a>)/Ue',"'$1'.str_replace('-',' ','$2').'$3'",$text); 
    Just remember some of the rules dealing with the e modifier http://us.php.net/manual/en/referenc....modifiers.php

    That is a good thought auriaks. My first thought was to try and use str_replace, but after the text for the link was fixed the link would no longer work.
    Last edited by james438; 02-22-2010 at 03:58 AM.
    To choose the lesser of two evils is still to choose evil. My personal site

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •