Issue with Preg_match_all I am new to this looks.

I thought that the below code would echo out hello world.
I have been struggling with this.
 I also want
the result that I get to be returned as a variable.


$test = "<title>hello world</title";
 $do4 = preg_match_all("<title>(.*?)</title>", $test, $matches4);
/////////////////////////////////////////
 ?>
<?
foreach ($matches4 as $val) {
    echo "matched: " . $val[0] . "\n";
}

Open in new window

LVL 3
jbrashear72Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ddrudikCommented:
$do4 = preg_match_all("/<title>(.*?)</title>/", $test, $matches4)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jbrashear72Author Commented:
hmm yea nothing showed.

<?
$test = "<title>hello world</title";
$do4 = preg_match_all("/<title>(.*?)</title>/", $test, $matches4);
/////////////////////////////////////////
 ?>
<?
foreach ($matches4 as $val) {
    echo "matched: " . $val[0] . "\n";
}
 
 
?>

Open in new window

0
hieloCommented:
You forgot the beginning and ending regular expression delimiters:
$test = '/<title>hello world</title/';
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

jbrashear72Author Commented:
Well in test I wont have that choice.. I am pulling in raw html data so Imagine the variable $test is an entire webpage.

I wont be able to ad delimiters into the source.
0
jbrashear72Author Commented:
So below is an example of bring in the URL I was trying to make it simple by using test just to test the preg_match_all.
<?
//$test = "<title>hello world</title>";
 $url = "http://www.myblog.com";
 $test = implode("", file($url));
 
$do4 = preg_match_all("/<title>(.*?)</title>/", $test, $matches4);
/////////////////////////////////////////
 ?>
<?
foreach ($matches4 as $val) {
    echo "matched: " . $val[0] . "\n";
}
 
 
?>

Open in new window

0
hieloCommented:
This worked for me:

$test = '<title>hello world</title>';
$do4 = preg_match_all('/<title>(.+?)<\/title>/', $test, $matches4);
/////////////////////////////////////////


print_r($matches4);

foreach ($matches4 as $val) {
    echo "matched: " . $val[0] . "\n";

}
0
jbrashear72Author Commented:
OK this code below worked.
Wonder what I did wrong. LOL

<?
//$test = "<title>hello world</title>";
 $url = "http://www.myblog.com";
 $test = implode("", file($url));
 
$do4 = preg_match_all("/<title>(.*?)<\/title>/", $test, $matches4);
/////////////////////////////////////////
 ?>
Show Title:
<?
foreach ($matches4 as $val) {
    echo "matched: " . $val[0] . "\n";
}
 
 
?>

Open in new window

0
ddrudikCommented:
Thanks for the question and the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.