• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 175
  • Last Modified:

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

0
jbrashear72
Asked:
jbrashear72
  • 4
  • 2
  • 2
2 Solutions
 
ddrudikCommented:
$do4 = preg_match_all("/<title>(.*?)</title>/", $test, $matches4)
0
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now