Jump to content

[SOLVED] preg match 2 items in paragraph


scarhand

Recommended Posts

lets say i have a paragraph like this:

 

rat pig bear

rat pig bear

rat pig bear

rat cat dog

rat pig bear

rat pig bear

rat cat monkey

rat pig bear

rat pig bear

 

i am trying to get "dog" and "monkey" from the preg_match array....

 

heres my code which doesnt work:

 

<?php

  preg_match('/cat (.+)$(.*)cat (.+)$/', $paragraph, $matches);
  $animal1 = $matches[1];
  $animal2 = $matches[2];

?>

 

im a total regex newb  :-\

Link to comment
https://forums.phpfreaks.com/topic/131680-solved-preg-match-2-items-in-paragraph/
Share on other sites

thanks, wasnt aware of preg_match_all

 

one more thing, i am trying to take content from old static pages and insert them into a database

 

now i am trying to get the content (which can be longer than 1 line) between each font tag so i can insert it into my database

 

the code i am using for that isnt working either:

 

<?php

  preg_match_all('/<font style="font-size: 14px;">(.*)</font>/', $oldpage, $bmatches);
  
  foreach ($bmatches as $value)
  {
    echo $value;
  }

i figured that out but the array contains nothing

 

the code looks like this:

 

<font style="font-size: 14px;">  there once
was a man who lived
on a farm </font>

 

heres my php code:

 

<?php

  preg_match_all('/<font style="font-size: 14px;">(.*)<\/font>/', $page, $bmatches);
  
  foreach ($bmatches as $value)
  {
    echo $value;
  }

?>

ugh i have one more problem

 

now the page has more than 1 instance of what im trying to get content from

 

example:

 

<font style="font-size: 14px;">  there once
was a man who lived
on a farm </font>

<font style="font-size: 14px;">  there once
was a man who lived
on a farm </font>

<font style="font-size: 14px;">  there once
was a man who lived
on a farm </font>

 

my array is outputting this:

 

  there once
was a man who lived
on a farm </font>

<font style="font-size: 14px;">  there once
was a man who lived
on a farm </font>

<font style="font-size: 14px;">  there once
was a man who lived
on a farm 

 

heres my php code:

 

<?php

  preg_match_all('/<font style="font-size: 14px;">(.*)<\/font>/s', $page, $bmatches);
  echo $bmatches[1][0];

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.