Jump to content

Recommended Posts

I am trying to modify my message board posts in a way that it will display any <img> images that are located in my smileys folder but only display a <a href> link if they are located anywhere else. So if someone makes a post using a smiley located in http://mysite/forum/smileys folder then it makes no change at all because it will already be in the <img> tags. But if it is a hotlink to an image on another site or even a hotlink to an image in a different folder on my own site I'd like to change it

from:

<img src="http://differentsite.com/image.jpg" alt="" border="0" />

 

to:

<a href="http://differentsite.com/image.jpg" target="_blank">http://differentsite.com/image.jpg</a>

 

The entire post is stored in the $message variable. How would I code this to search the variable for instances of <img> tag links and ignore it if it is in the http://mysite/forum/smileys folder but change it to an <a href> link if it points to any other folder or website?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/157628-need-some-help-with-message-board-posts/
Share on other sites

Start looking into regular expression. You are wanting to search for a pattern and if it matches do something with it based on what it is.

 

Regular expressions are exactly what this is for. Good Luck.

 

I figured that is what I would need but was hoping for a different approach. Regex is the most complicated thing I have seen in PHP.

Yes is it a very complicated process, and I still don't understand it worth nothing.

 

But what your wanting to do is a very complicated process. Your trying to find a particular string that could change, and then do something based on what you find.

 

Regular expressions are the only way to go that I know of.

 

Nate

Regex is the most complicated thing I have seen in PHP.

 

It's really not that bad and is quite easy after some practice. I can move this to the Regex board if you'd like. I know they are always hungry for a new challenge there.

 

Please do. Thanks.

So if I understand you correctly, you want to examine all <img> tags in a post, and if any of their src attributes don't hold a specific value (like in your example, 'http://mysite/forum/smileys', you want to replace those image tags with anchor tags which contain those img src values?

 

So something like this?


$message = 'This image: <img src="http://differentsite.com/image.jpg" alt="" border="0" /> or this image: <img src="http://mysite/forum/smileys" alt="" /> or <img src="http://mysite/some/folder/smileys" alt="" />';

if(preg_match_all('#<img.+?src=([\'"])([^\'"]+)\1.*?/>#i', $message, $matches)){
$count = count($matches[0]);
for ($a = 0 ; $a < $count ; $a++) {
	if(strpos($matches[2][$a], 'http://mysite/forum/smileys') === false){ // change this path to your site path
		$message = str_replace($matches[0][$a], '<a href="' . $matches[2][$a] . '" target="_blank">'. $matches[2][$a] . '</a>', $message);
	}
}
}

echo $message;

 

Output (via richt-click 'view source'):

This image: <a href="http://differentsite.com/image.jpg" target="_blank">http://differentsite.com/image.jpg</a> or this image: <img src="http://mysite/forum/smileys" alt="" /> or <a href="http://mysite/some/folder/smileys" target="_blank">http://mysite/some/folder/smileys</a>

 

Just a note on target="_blank". In general, target is frowned up, as this removes control from the user (accessibility issues). Additionally, according to this w3c page,  if you are using a strict doc type, don't even bother with target, as it is deleted:

 

In HTML 4.01 Transitional and XHTML 1.0 Transitional, the target attribute can be used to open a new window, instead of automatic pop-ups. (The target attribute is deleted from HTML 4.01 Strict and XHTML 1.0 Strict.)

You understood my request perfectly. I will give that a whirl this evening when I get home from work. I appreciate your time.

 

I am using the same forum software that phpfreaks uses (SMF) and they have that _blank attribute built into it already so that is really out of my control.

So if I understand you correctly, you want to examine all <img> tags in a post, and if any of their src attributes don't hold a specific value (like in your example, 'http://mysite/forum/smileys', you want to replace those image tags with anchor tags which contain those img src values?

 

So something like this?


$message = 'This image: <img src="http://differentsite.com/image.jpg" alt="" border="0" /> or this image: <img src="http://mysite/forum/smileys" alt="" /> or <img src="http://mysite/some/folder/smileys" alt="" />';

if(preg_match_all('#<img.+?src=([\'"])([^\'"]+)\1.*?/>#i', $message, $matches)){
$count = count($matches[0]);
for ($a = 0 ; $a < $count ; $a++) {
	if(strpos($matches[2][$a], 'http://mysite/forum/smileys') === false){ // change this path to your site path
		$message = str_replace($matches[0][$a], '<a href="' . $matches[2][$a] . '" target="_blank">'. $matches[2][$a] . '</a>', $message);
	}
}
}

echo $message;

 

Output (via richt-click 'view source'):

This image: <a href="http://differentsite.com/image.jpg" target="_blank">http://differentsite.com/image.jpg</a> or this image: <img src="http://mysite/forum/smileys" alt="" /> or <a href="http://mysite/some/folder/smileys" target="_blank">http://mysite/some/folder/smileys</a>

 

Just a note on target="_blank". In general, target is frowned up, as this removes control from the user (accessibility issues). Additionally, according to this w3c page,  if you are using a strict doc type, don't even bother with target, as it is deleted:

 

In HTML 4.01 Transitional and XHTML 1.0 Transitional, the target attribute can be used to open a new window, instead of automatic pop-ups. (The target attribute is deleted from HTML 4.01 Strict and XHTML 1.0 Strict.)

 

Wow. You are pure genius. It's akin to witchcraft how you guys can make things happen. Thanks.

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.