Jump to content

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

<pre>
<?php
$data = <<<DATA
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
DATA;
preg_match_all('/(?<=\scat\s).+$/m', $data, $matches);
print_r($matches);
?>
</pre>

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];

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.