473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

[regex] Why doesn't preg_replace work?

Hello

I went through some examples, tried a bunch of things... but still
can't figure out why I can't extract the TITLE section of a web page
using preg_replace():

-----------
<?php

$url = "http://www.cnn.com";

$response = file_get_contents($url);

$output=preg_replace("|<title>(.+?)</title>|smiU",
"TITLE=$1",
$response);

$fp = fopen ("output.html", "w");
fputs ($fp,$output);
fclose($fp);
-----------

Any idea?

Thanks!
May 7 '07 #1
2 2061
Hi Gilles,

I'm not a regex guru, but I can see a spot a couple problem areas in
your expression:

1. The core syntax could probably be simplified using something like
this:

|^<title>([^<]+)</title>$|i

I hope I got that right -- I usually have to test my expression a few
times before I get all the nuances right. :)

2. smiU - That's modifier overkill. The U here and the ? in your
expression are probably reacting to each other in unexpected ways. If
you don't know about this page, it can help:

http://www.php.net/manual/en/referen....modifiers.php

I have a prefab function I've used for this very thing, but
unfortunately I don't have access to it that moment. Hopefully,
someone will be along shortly with the proper syntax. In the
meantime, I hope this helps in a more general sense.

Regards,
Tom

On May 7, 4:30 pm, Gilles Ganault <nos...@nospam.comwrote:
Hello

I went through some examples, tried a bunch of things... but still
can't figure out why I can't extract the TITLE section of a web page
using preg_replace():

-----------
<?php

$url = "http://www.cnn.com";

$response = file_get_contents($url);

$output=preg_replace("|<title>(.+?)</title>|smiU",
"TITLE=$1",
$response);

$fp = fopen ("output.html", "w");
fputs ($fp,$output);
fclose($fp);
-----------

Any idea?

Thanks!

May 7 '07 #2
On 7 May 2007 16:46:27 -0700, klenwell <kl******@gmail.comwrote:
>2. smiU - That's modifier overkill. The U here and the ? in your
expression are probably reacting to each other in unexpected ways.
Ah, ah... Indeed, it seems like it's either using the U switch to make
Preg non-greedy, or use the ? limiter (eg. ".+?"). Thanks for pointing
it out.

Found it: To extract bits, I shouldn't use preg_replace() but
preg_match():

--------------
$url = "http://www.cnn.com";
$response = file_get_contents($url);

preg_match("|<title>(.+?)</title>|smi",$response,$matches);
$response = $matches[1];

$fp = fopen ("output.html", "w");
fputs ($fp,$response);
fclose($fp);
--------------

Thank you.
May 8 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Alexandre Lahure | last post by:
Hi all I'd like to execute a piece of code when I find a particular string. So I used preg_replace('/my_regex/e', 'my_piece_of_code', $my_string) Actually, I'd like to convert something like...
1
by: The Plankmeister | last post by:
I'm sure this should work: echo preg_replace("/(\w)(\!w)(\w)/","\\1\. \\3","this is.a test.to see. if it. works"); It should output: this is. a test. to see. if it. works But it doesn't....
6
by: Brian Richmond | last post by:
I'm trying to use a regular expression to match a hidden html tag and replace it with the results of a mysql query. The query is based off a part of the hidden tag. For example: The article...
3
by: Red | last post by:
In netscape bookmark files, there are lots of lines like this: <DT><A HREF="http://www.commondreams.org/" ADD_DATE="1091500674" LAST_CHARSET="ISO-8859-1" ID="rdf:#$uiYyb3">Common Dreams</A> I...
5
by: Markus Ernst | last post by:
Hello I have a regex problem, spent about 7 hours on this now, but I don't find the answer in the manual and googling, though I think this must have been discussed before. I try to simply...
8
by: erikcw | last post by:
Hi all, I'm trying to write a regex pattern to use in preg_replace. Basically I want to put around every line (\n) in this variable. However, I need to exclude lines that already have brackets...
1
by: semi_evil | last post by:
I'm trying to achieve the following: start form entry user enters form data and submits ($form_data) preg_match_all data for $pattern, store matches in $match_arr if one or more matches...
7
by: GoL | last post by:
Hi all, I have a problem that I *think* is simple, but since I'm completely ignorant about regex expression I don't have a clue how to do it... What I need to do is to find all instances in a...
5
by: nel | last post by:
I have two tags: <!--// Remove Begin //--and <!--// Remove End //--> I want to use regi_replace() to remove everything between these tags. The thing is, these tags can be repeated throughout...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.