Jump to content

Replace url with name


EchoFool

Recommended Posts

Hey,

 

How would you do a replacement on a string which changes ?

 

For example:

 

<a href="users.php?id=3">Username</a>

 

How would you use str_replace to take out <a href="users.php?id=3"> AND </a>

 

Note - the id=3 could be any random number so i can't use exact strings to replace cos the value could be different.

 

Thanks hope you can help.

Link to comment
https://forums.phpfreaks.com/topic/208003-replace-url-with-name/
Share on other sites

I'm sure you could do it easily with preg_replace or using strpos() and str_replace() and substr().

 

 

something like this would work, but i wouldn't rely on it. i'd go for preg_replace if someone experienced with regular expressions is kind enough to share their knowledge?

$string = '<a href="users.php?id=3">Username</a>';

//get rid of closing </a>
$string = str_replace("</a>", "", $string);

//get position of end of anchor tag. add 2 to str pos because it returns start of ">
$closePos = strpos($string, '">') + 2;
$string = substr($string, $closePos, 100);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.