Regular expression

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Regular expression

Post by user___ »

Hi guys,
I have been given a group of .html files and I have to extract some image names from them.

.html files are similar.
This is a sample of one of them:

Code: Select all

<html>
<head>
//I do not copy all meta tags, javascripts, css, etc. here because they are too much
</head>
<body>
//Here are some tables and other tags but I do not copy them because of their size too
<a href="javascript: open_win('images.php?img=images/houses/b3ec038139m27saed706a436d958550d.jpg&alt=b3ec038139m27saed706a436d958550d.jpg',);" ><img alt="house" src="photos/thumbs/b3ec038139m27saed706a436d958550d.jpg" border=0></a>
//Other tags go here
</body
</html>
I need to extract 'b3ec038139m27saed706a436d958550d' from them.

BTW:In any file these values are different.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

We have a whole board dedicated to Regex, you know.

What have you tried so far?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

and what is your question?
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Reply

Post by user___ »

My question is how to get that value?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Get the file, then use regex. What have you tried so far?
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Reply

Post by user___ »

I have tried exctly what you have just said. I get the file and then use a regexp but the regexp does not return what it is supposed to.

This is the regexp:$name = preg_replace('/(.*?)photos\/thumbs\/(.*?).JPG(.*?)/i', '$2', $str);


BTW:I remove all new lines when I get the contents of a file.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Reply

Post by superdezign »

user___ wrote:This is the regexp:$name = preg_replace('/(.*?)photos\/thumbs\/(.*?).JPG(.*?)/i', '$2', $str);
The first and last (.*?) are completely useless and unnecessary.
user___ wrote:but the regexp does not return what it is supposed to.
It looks to me like it supposed to return the entire string with only "photos/thumbs/foo.jpg" replaced with "foo." What is it returning for you?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

This shouldn't need preg_replace(). preg_match(), likely or preg_match_all() in the worst case.
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Reply

Post by user___ »

I have fixed it. The problem was that when I tried to get the value it was printed more than once. I mean that it was something like this:
thevalue<a href="..."></a>...
thevalue has a specific length so I just cut the string and I had it done.
Post Reply