aeroswat Posted August 9, 2010 Share Posted August 9, 2010 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 '>? Quote Link to comment https://forums.phpfreaks.com/topic/210266-is-there-a-function-that-will-find-the-text-between-two-strings/ Share on other sites More sharing options...
gizmola Posted August 9, 2010 Share Posted August 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210266-is-there-a-function-that-will-find-the-text-between-two-strings/#findComment-1097249 Share on other sites More sharing options...
aeroswat Posted August 9, 2010 Author Share Posted August 9, 2010 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 Thanks. Any tips for a good tutorial site on them? Quote Link to comment https://forums.phpfreaks.com/topic/210266-is-there-a-function-that-will-find-the-text-between-two-strings/#findComment-1097251 Share on other sites More sharing options...
aeroswat Posted August 9, 2010 Author Share Posted August 9, 2010 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.... Quote Link to comment https://forums.phpfreaks.com/topic/210266-is-there-a-function-that-will-find-the-text-between-two-strings/#findComment-1097260 Share on other sites More sharing options...
gizmola Posted August 10, 2010 Share Posted August 10, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210266-is-there-a-function-that-will-find-the-text-between-two-strings/#findComment-1097281 Share on other sites More sharing options...
aeroswat Posted August 10, 2010 Author Share Posted August 10, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210266-is-there-a-function-that-will-find-the-text-between-two-strings/#findComment-1097570 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.