help with a string.

Wow, how the board has changed…

I used to come here all of the time, and then couldn’t, due to the type of work I did.

Some of you may remember me, but not likely.

I am going to be trying to come back as often as possible, too.

I have since my last visit, I have, with a partner, just launched a site called windsorinsider.ca. It is half news site, and half business listing site. We are keeping everything local to our community. We are also going to be competing with the local newspaper here. That’s the fun part.

Ckeck it out.

But the real reason I am here today, is to see about getting some help with a string of characters.

I am storing articles in a table, and the story feild in Mysql stores all of the html for images.

I am trying to get the img tag out of the data, and even manipulate that further.

If I wanted to get :

out of a the story string of text, then remove the height, width and class elements, and copy the title out and store that in a variable. what would you suggest using?

I have been working with strings, but can’t get this one.

Any help is much appreciated.

Thanks guys, and see you soon.

There are actually two tasks, not one - first is to eliminate attributes what you mentioned, second is to extract title and put it in a variable.

For the first task, this regular expression will make replacements what you need for every matched entry in the content (though the order of the attributes is important):

[php]
$new_content = preg_replace("~(<[\s]?img[\s]+)(height="[\d]+"[\s])?(width="[\d]+"[\s])?(class="[^"]"[\s])?(title="[^"]"[\s]+)?(src="[^"]"[\s])?(alt="[^"]"[\s])?([\s]+/>)~i","\1\5\6\8",$content);
[/php]

As for extracting title to a variable, you can use preg_match_all to find all image titles in the content:

[php]
if(preg_match_all("~<[\s]?img[\s]+[^>]title="([^"])"[^>]/>~i",$content,$matches)){
print_r($matches[1]);
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service