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
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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.