Replace all Links title...

Discussion in 'PHP' started by mehdi, Aug 16, 2009.

  1. #1
    Hello, I want to build a function which replaces all the links title in a string to something this:

    Original: Check out my website at: <a href="test.com">Test.com</a>
    After Replace: Check out my website at: <a href="test.com">Test.com (Replaced)</a>

    Please can anyone help me, make sure that if the links contains any class or any other tag then they also replace them... like if the link is <a class="link" href="test.com">, then it must also work...

    Thanks..!!!
     
    mehdi, Aug 16, 2009 IP
  2. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    kblessinggr, Aug 16, 2009 IP
  3. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #3
    $str = preg_replace('#<a([^>]+)>(.+?)</a>#is', '<a$1>$2 (replaced)</a>', $str);
    Code (markup):
     
    joebert, Aug 16, 2009 IP
    mehdi likes this.
  4. j4jawaid

    j4jawaid Peon

    Messages:
    94
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Enjoy dude if you feel any problem in understanding it. just ask me i am here to help. :)


    <?php
    
    	$str='Check out my website at: <a href="mjawaid.co.cc">MJawaid.Co.Cc</a>';
    	
    	$replace_str="MJG";
    	
    	$newstr= replace_title($str,$replace_str);
    	
    	echo $str;
    	echo "<br>";
    	echo $newstr;
    	
    	
    	
    	function replace_title($str,$replace_str)
    	{
    		$a=strpos($str,'">');
    		$a+=2;
    		$b=strpos($str,"</");
    		$str=substr_replace($str,$replace_str,$a,$b-$a);
    		return $str;
    	}
    
    ?>
    Code (markup):
     
    j4jawaid, Aug 16, 2009 IP
    mehdi likes this.
  5. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks Joebert i was looking for that but can you please customize your code so that it will become like this:
    Original: Check out my website at: <a href="test.com/mypage.html">Test Link</a>
    After Replace: Check out my website at: <a href="test.com/mypage.html">Test Link (http://test.com/mypage.html)</a>
     
    mehdi, Aug 16, 2009 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    Is there anything else you're not mentioning, I don't want to do this again after this time. :)
     
    joebert, Aug 16, 2009 IP
  7. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I am not sure... :D, but I will try myself first, before asking you.
     
    mehdi, Aug 16, 2009 IP
  8. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #8
    $str = preg_replace('#<a([^>]+)href=([\'"])([^\2]+)\2([^>]*>)(.+?)</a>#is', '<a$1href=$2$3$2$4$5 ($3)</a>', $str);
    Code (markup):
     
    joebert, Aug 16, 2009 IP
  9. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #9
    :p what would happen if I were to move the position of the href attribute to the end or middle or somewhere out of order with the last link?
     
    kblessinggr, Aug 16, 2009 IP
  10. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Well, looks like he just made a small mistake:

    $str = preg_replace('#<a(.*?)href=([\'"])([^\2]+)\2([^>]*>)(.+?)</a>#is', '<a$1href=$2$3$2$4$5 ($3)</a>', $str);
    PHP:
     
    premiumscripts, Aug 16, 2009 IP
    mehdi likes this.
  11. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Sorry, i tried that code but that was not working...
    
    <?php
    $str = '<a href="http://mehdi.com">Mehdi Website</a> <a href="http://mehdi2.com">Mehdi Website2</a>';
    $str = preg_replace('#<a(.*?)href=([\'"])([^\2]+)\2([^>]*>)(.+?)</a>#is', '<a$1href=$2$3$2$4$5 ($3)</a>', $str);
    echo $str;
    ?>
    
    PHP:
    Shows me: Mehdi Website Mehdi Website2 (http://mehdi.com">Mehdi Website
     
    mehdi, Aug 16, 2009 IP
  12. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #12
    Seems like a bug to me, but third match was greedy, although I'm pretty sure it shouldn't have been

    Try this little fix of premiumscripts' pattern (changed ([^\2]+) to ([^\2]+?))

    
    $str = preg_replace('#<a(.*?)href=([\'"])([^\2]+?)\2([^>]*>)(.+?)</a>#is', '<a$1href=$2$3$2$4$5 ($3)</a>', $str);
    
    PHP:
     
    Gray Fox, Aug 16, 2009 IP
    mehdi likes this.
  13. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Thanks Guyz, Its working good Now :D... REP ADDED TO ALL!
     
    mehdi, Aug 16, 2009 IP
  14. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #14
    I'm glad so many people were able to catch on to where that pattern was going. :D
     
    joebert, Aug 16, 2009 IP