preg_match???

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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

preg_match???

Post by malcolmboston »

ok, im building a code that will automatically rename a file to its <title>

this is useful when you download a site that dynamically generates its content (for example viewtopic.php?topic=435345)

now ive built it but am having problems in the most important bit, lol

ok, ive put the whole file into an array and just now need the formula, does anyone have any ideas?

for example $title = preg_match(<title>i need this</title>);
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Thsi should do it. It isn't preg_match BTW

Code: Select all

$string = "<title>something</title>";

$title = preg_replace("|<[\/\!]*?[^<>]*?>|si", "", $string);

print $title;
Mark
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

I think you do need preg_match :o
Eg

Code: Select all

<?php
$file = file_get_contents('http://forums.devnetwork.net/viewtopic.php?p=120170#120170');
preg_match('/<title>(.[^<]*)<\/title>/i', $file, $matches);
$filename = empty($matches[1]) ? 'Unknown' : $matches[1];
echo $filename;
?>
Last edited by markl999 on Fri Jul 09, 2004 9:49 am, edited 1 time in total.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Bech100 wrote:Thsi should do it. It isn't preg_match BTW

Code: Select all

$string = "<title>something</title>";

$title = preg_replace("|<[\/\!]*?[^<>]*?>|si", "", $string);

print $title;
Mark
As I know you are in the process of trying to learn regex, it may be good if you could try and detail/describe your regex to us, it may help us point out where you are going wrong.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

ah, i c.

I didn't read the bit about reading the entire HTML, i though he was just starting with "<title>i need this</title>".

:)

Mark
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

redmonkey wrote:As I know you are in the process of trying to learn regex, it may be good if you could try and detail/describe your regex to us, it may help us point out where you are going wrong.
The regex shown does work.

I didn't realise that it was an entire HTMl document to start with that we were search.

Mark
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

I haven't tested it but it looks like it would return $title as '>something>'

You also have alot of unesseccary characters in there.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

i have tested it and it does work.

As you say, im still getting used to this stuff. I was just seeing how i could strip all html tags from something, and that is what i came up with.

So it isn't actually tailored for this job, it would work for any text contains within and type of tag.

Im not sure how this works though

Code: Select all

preg_match('/<title>(.[^<]*)<\/title>/i', $file, $matches);


Mark
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

preg_match('/<title>(.[^<]*)<\/title>/i', $file, $matches); just means match any characters in between <title> and </title> that isn't a <
The [^<] makes it non greedy.
Post Reply