SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    custom tag of <url> without any delimiter or with delimiter "space"

    Code:
    $myString1="I often visit <url>www.sitepoint.com/forums</url> 
    and <url>www.google.com</url>.";
    
    $myString2="I often visit <url>www.sitepoint.com/forums sitePointForums</url>
    and <url>www.google.com google</url>.";
    
    $myString1=preg_replace('/<url>(.*?)<\/url>/s',
     '<a href="http://$1">$1</a>', $myString1);
    
    $myString2=preg_replace('/<url>(.*?) (.*?)<\/url>/s', 
    '<a href="http://$1">$2</a>', $myString2); 
    
    echo $myString1.'<hr>';
    
    echo $myString2.'<hr>';
    
    result
    I often visit <a href="http://www.sitepoint.com/forums">www.sitepoint.com/forums</a> 
    and <a href="http://www.google.com">www.google.com</a><hr>
    I often visit <a href="http://www.sitepoint.com/forums">sitePointForums</a> 
    and <a href="http://www.google.com">google</a><hr>
    I can say blue parts are urls and red parts are link texts.
    In myString2, there are spaces as delimiters between www.sitepoint.com/forums and sitePointForums or www.google.com and google which are inside the start <url> tag and the end </url> tag.

    On the contrary, myString1 has no space between the start <url> tags and the end </url> tags.

    In myString1, urls become directly link texts.



    If I have myString3 which has links with delimiters and without delimiters like the below,
    how can I make my target result below when a custom tag of <url> has space as a delimiter make the link text with the text after delimiter
    and when a custom tag of <url> has no space make the link text with the url itself.

    Code:
    $myString3="I often visit <url>www.sitepoint.com/forums</url> 
    and <url>www.google.com google</url>.";
    
    result
    
    I often visit 
    <a href="http://www.sitepoint.com/forums">www.sitepoint.com/forums</a>
    and <a href="http://www.google.com">google</a><hr>

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    Run both patterns over the string. (Hint: run pattern 2 first)

  3. #3
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $myString3="I often visit <url>www.sitepoint.com/forums</url> 
    and <url>www.google.com google</url>.";
    
    $myString3=preg_replace('/<url>(.*?) (.*?)<\/url>/s', '<a href="http://$1">$2</a>', $myString3); 
    
    $myString3=preg_replace('/<url>(.*?)<\/url>/s', '<a href="http://$1">$1</a>', $myString3);
    
    echo $myString3.'<hr>';
    The code above which is written pattern2 first get the result3 below.

    result3
    Code:
    I often visit <a href="http://www.sitepoint.com/forums</url>">
    and <url>www.google.com google</a>.<hr>
    On the contrary, my target result is like the below
    target result
    Code:
    I often visit <a href="http://www.sitepoint.com/forums">sitePointForums</a> 
    and <a href="http://www.google.com">google</a><hr>

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    Try using this as your first pattern instead:

    "#<url>([^\s]*?) ([^<]*?)</url>#s"

  5. #5
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much, StarLion. Your code works nice.


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
  •