kyleabaker Posted September 24, 2009 Share Posted September 24, 2009 I've searched all over and can't seem to find anything that seems to work for what I need. I've got preg_replace() working a little, but not as well as I need it to work. What I'm trying to accomplish is to take a string that contains html and convert most of the tags from html tags to bbcode (mainly just the basics; img, a, b, etc.). So for the <img> tag I've got it working a little for replacing basic img tags, but I need it to be dynamic so that if the tags aren't arranged in a specific order then it will still be recognized and parsed. <img src="http://www.example.domain/image.png"> <img src="http://www.example.domain/image.png" alt="blah"> <img id="something" src="http://www.example.domain/image.png" style="styling-here"> These should all be detected and replaced with... [img=http://www.example.domain/image.png] This seems easy enough, but it's giving me trouble. What I have so far is this: $content = preg_replace("/<img src=\"([ a-zA-Z0-9|\/\:\+.%?=_-]*)\">/i", "[img=$1]", $content); That seems to work great for the first image style, but I need it to match the others as well. I know there is a better way of doing this, but that's the cleanest I could make this code. Can someone help me get this working to match all of these images and just place the url for the img in the bbcode tag? Thanks in advanced. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 24, 2009 Share Posted September 24, 2009 preg_replace('~<img\b[^>]+\bsrc\s?=\s?[\'"](.*?)[\'"]~is', '', $str); Will find the src attribute regardless of position. The word boundaries (\b) makes sure the tag name and attribute not are parts of longer names, so it won't match 'fake' tags like <imgfake. Just ask if you need something else explained. Edit: The replacement should be '', but it's removed when I try to post it. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted September 24, 2009 Share Posted September 24, 2009 Edit: The replacement should be '', but it's removed when I try to post it. Are you having posting issues as well? Which browser are you using if this is the case? I find that for this site, Chrome and Firefox give deleted/altered posting elements in the results (especially when previewing a post), where as Opera doesn't. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 24, 2009 Share Posted September 24, 2009 Yup. Using Firefox 3.5.3. I just assumed it had something to do with the BBcode in my case. And after testing it, seems it does. [/img] can be posted, but when something is entered between the tags, the whole lot disappears. Quote Link to comment Share on other sites More sharing options...
kyleabaker Posted September 24, 2009 Author Share Posted September 24, 2009 preg_replace('~<img\b[^>]+\bsrc\s?=\s?[\'"](.*?)[\'"]~is', '', $str); Will find the src attribute regardless of position. The word boundaries (\b) makes sure the tag name and attribute not are parts of longer names, so it won't match 'fake' tags like <imgfake. Just ask if you need something else explained. Edit: The replacement should be '', but it's removed when I try to post it. Excellent. Thanks so much for the help...and so quickly! Edit: If you use the code tag it won't remove that code. It seems the php bbcode tag will though. Quote Link to comment 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.