Results 1 to 4 of 4

Thread: Small trouble with regular expressions

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

    Default Small trouble with regular expressions

    Hey guys!
    Please will somebody help me with this?
    Link now looks like this:

    <a href="this-is-some-folder/word1-word2-word3.html" target="_blank">this is some folder/word1 word2 word3</a>

    and I need to be without ’this is some folder/’ (highlighted with red) like this

    <a href="this-is-some-folder/word1-word2-word3.html" target="_blank">word1 word2 word3</a>

    This is my PHP code:

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

    $text = preg_replace('/ link_in_folder:(\S+)/e', '" <a href=\""."\\1.html\""." target=\"_blank\">".str_replace("-"," ","\\1")."</a>"', $text);
    $text = preg_replace('/(<a.+>)(\.+)(<\/a>)/Ue',"'$1'.preg_replace('{.+/}','','$2').'$3'",$text);
    return $text;
    }

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

    I would appreciate little help with this one.
    Thank you advance
    Cheers!

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

    Default

    Hi,

    I didn't look too closely at all of your code, but try replacing
    PHP Code:
    $text preg_replace('/(<a.+>)(\.+)(<\/a>)/Ue',"'$1'.preg_replace('{.+/}','','$2').'$3'",$text); 
    with
    PHP Code:
    $text preg_replace('/(<a.*?>).*\/(.*?<\/a>)/','$1$2',$text); 
    To choose the lesser of two evils is still to choose evil. My personal site

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

    Default

    I am not sure how to ask this, but out of curiosity what is your code supposed to do? How is it used by the user?
    To choose the lesser of two evils is still to choose evil. My personal site

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

    Default

    One nice man on other forum helped me.. this works...

    $output = preg_replace('%(<a\b[^>]+>)(.*?/)([^/<]+</a>)%', '$1$3', $input);

    And this is something that I made for my wordpress blog… for interlinking and similar things… I know that there are some plugins which doing same thing, but I don’t like the way they working…
    Last edited by young_coder; 02-26-2010 at 09:34 PM.

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
  •