Jump to content

Is there a function that will find the text between two strings?


aeroswat

Recommended Posts

I'm not exactly sure how to go about this but I'm trying to handle some bbcode for a url. So the php is getting some text like this

 

[link=www.website.com]Website[/link]

 

And I am using str_replace to replace [link= with <a href=' but how do I replace the closing ] with '>?

This type of problem is best solved with regular expressions because you're looking for patterns that include variables, and regex can capture the variable stuff (in this case the url) making it easy to output your html version.  Take a look at preg_replace() instead.

This type of problem is best solved with regular expressions because you're looking for patterns that include variables, and regex can capture the variable stuff (in this case the url) making it easy to output your html version.  Take a look at preg_replace() instead.

 

I thought I might have to use reg exp's. That sucks because I don't understand them lol. Guess i'll have to go back to learning them :P Thanks. Any tips for a good tutorial site on them?

Ok here's what I got for the regex...

$str = preg_replace('/\[link=(.*?)\](.*?)\[\/link\]/','<a href="$1">$2</a>',$str);

There is a problem though. For some reason firefox is appending my root directory to the url when it's www.website.com....

A valid href needs to have the http:// in front of it.  You will need to check their link for it at the beginning and if they don't have it you can add it in.

 

Yes sir. I decided to make a check for the http and have an example of it in the prompt box. Thanks for the help.

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.