Jump to content

Help with preg_replace replacing <img> with [img]


kyleabaker

Recommended Posts

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.  :shrug:

 

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.

Link to comment
Share on other sites

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 '$1', but it's removed when I try to post it.

Link to comment
Share on other sites

Edit: The replacement should be '$1', 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.

Link to comment
Share on other sites

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 '$1', 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.

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.