Page 1 of 1

preg_match???

Posted: Fri Jul 09, 2004 8:43 am
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>);

Posted: Fri Jul 09, 2004 9:35 am
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

Posted: Fri Jul 09, 2004 9:40 am
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;
?>

Posted: Fri Jul 09, 2004 9:46 am
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.

Posted: Fri Jul 09, 2004 9:47 am
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

Posted: Fri Jul 09, 2004 10:05 am
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

Posted: Fri Jul 09, 2004 10:09 am
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.

Posted: Fri Jul 09, 2004 10:13 am
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

Posted: Fri Jul 09, 2004 10:16 am
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.