SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  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)

    Using delimiter "space" in making a link with a custom tag of <url>

    Code:
    code
    $myString="I often visit <url>www.sitepoint.com/forums</url> and love it.";
    
    $myString=preg_replace('/<url>(.*?)<\/url>/s', 
    '<a href="http://$1">http://$1</a>',
     $myString); 
    
    echo $myString;
    
    result
    
    I often visit 
    <a href="http://www.sitepoint.com/forums">http://www.sitepoint.com/forums</a> 
    and love it.
    The code above works fine with the result above.

    I like to make it like the following.

    if there is any space between <url> and </url>, the string which is after the first space will be the link title.

    The would-be code and my target result will be like the following.
    Code:
    would-be code
    
    $myString="I often visit 
    <url>www.sitepoint.com/forums sitepointForums</url> 
    and love it.";
    
    $myString=preg_replace('/<url>(.*?)<\/url>/s', 
    '<a href="http://$1">$afterSpace</a>', $myString); 
    
    target result
    
    I often visit 
    <a href="http://www.sitepoint.com/forums">sitepointForums</a> 
    and love it.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    $myString="I often visit <url>www.sitepoint.com/forums Sitepoint Forums</url> and love it.";

    $myString=preg_replace('/<url>(.*?) (.*?)<\/url>/s'
    '<a href="http://$1">$2</a>',
     
    $myString); 

    echo 
    $myString;


    // I often visit <a href="http://www.sitepoint.com/forums">Sitepoint Forums</a> and love it. 
    Had to escape the / here <\/url>

  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)
    Quote Originally Posted by Cups View Post
    $myString=preg_replace('/<url>(.*?) (.*?)<\/url>/s',
    '<a href="http://$1">$2</a>',
    $myString);

    echo $myString;
    Thank you very much, Cups.
    Your code works fine.


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
  •