[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

Loop in php

Hi I have the attached script which identifies a phrase within text provide and then echos that phrases along with 3 words in front of it and 3 words after it.

The result is...

... secret of a Cape Cod vacation close to their ...

BUT, I would like it to execute that for each instance of the phrase in the text.. so I would like the result to echo exactly the following for the text in $subject...

... secret of a Cape Cod vacation close to their ...
... trek for a Cape Cod vacation, many more people ...
... you’re considering a Cape Cod vacation, here are some ...

Could someone please help me achieve that based off of this code?

Thanks very much!




<?php

$subject= "For decades, families in the Boston area held the secret of a Cape Cod vacation close to their hearts like
a family treasure. Every Friday, scores of station wagons loaded with escapees from the city would file
over the Bourne Bridge to the East Coast’s most enchanting vacation spot. Today, the word is out.
While local Bostonians still make the trek for a Cape Cod vacation, many more people are crowding onto
this coastal gem to take advantage of its amazing Atlantic views, superb seafood, and unique New
England attitude. If you’re considering a Cape Cod vacation, here are some of the venues and attractions
you should put on your list.";
$pattern= "/(\w+\s){3}Cape Cod Vacation(\s\w+){3}/";

preg_match($pattern, $subject, $matches);

echo("... $matches[0] ...");
?>

Open in new window

0
chrisj1963
Asked:
chrisj1963
  • 7
  • 7
1 Solution
 
Tyler LaczkoCommented:
<?php

$subject= "For decades, families in the Boston area held the secret of a Cape Cod vacation close to their hearts like
a family treasure. Every Friday, scores of station wagons loaded with escapees from the city would file
over the Bourne Bridge to the East Coast’s most enchanting vacation spot. Today, the word is out.
While local Bostonians still make the trek for a Cape Cod vacation, many more people are crowding onto
this coastal gem to take advantage of its amazing Atlantic views, superb seafood, and unique New
England attitude. If you’re considering a Cape Cod vacation, here are some of the venues and attractions
you should put on your list.";
$pattern= "/(\w+\s){3}Cape Cod Vacation(\s\w+){3}/";

preg_match($pattern, $subject, $matches);

foreach($matches as $val)
echo("... $val ...");
?>
0
 
chrisj1963Author Commented:
Hi,
Thank you for the quick response.   Unfortunately the result is empty

shows only                ...  ...

any other suggestions?
<?php

$subject= "For decades, families in the Boston area held the secret of a Cape Cod vacation close to their hearts like a family treasure. Every Friday, scores of station wagons loaded with escapees from the city would file over the Bourne Bridge to the East Coast’s most enchanting vacation spot. Today, the word is out. While local Bostonians still make the trek for a Cape Cod vacation, many more people are crowding onto this coastal gem to take advantage of its amazing Atlantic views, superb seafood, and unique New
England attitude. If you’re considering a Cape Cod vacation, here are some of the venues and attractions you should put on your list.";
$pattern= "/(\w+\s){3}Cape Cod Vacation(\s\w+){3}/";

preg_match($pattern, $subject, $matches);

foreach($matches as $val);
echo("... $val ...");
?>

Open in new window

0
 
Tyler LaczkoCommented:
I was using your original code. it doesnt work fyi.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Tyler LaczkoCommented:
If you fix your code to perform as you stated in the question the foreach loop will give you the results you need.
0
 
Tyler LaczkoCommented:
foreach($matches as $val)
    echo "... " . $val . " ...<br />";
0
 
chrisj1963Author Commented:
Hi thanks for you response again..... Regarding the code, it works for me... http://70.87.107.194/~g3crmco/aphrasetest.php 

do you have any other input on how to make it work. if no, please comment as such and I'll move on..

<?php

$subject= "For decades, families in the Boston area held the secret of a Cape Cod vacation close to their hearts like a family treasure. Every Friday, scores of station wagons loaded with escapees from the city would file over the Bourne Bridge to the East Coast’s most enchanting vacation spot. Today, the word is out. While local Bostonians still make the trek for a Cape Cod vacation, many more people are crowding onto this coastal gem to take advantage of its amazing Atlantic views, superb seafood, and unique New England attitude. If you’re considering a Cape Cod vacation, here are some of the venues and attractions you should put on your list. ";
$pattern= "/(\w+\s){3}Cape Cod vacation(\s\w+){3}/";

preg_match($pattern, $subject, $matches);

echo("... $matches[0] ...");
?>

Open in new window

0
 
chrisj1963Author Commented:
Oh... i just saw that you follow up with 2 additional comments... I did add your code... see http://70.87.107.194/~g3crmco/aphrasetest2.php but the result just says

...their...
http://70.87.107.194/~g3crmco/aphrasetest2.php

Open in new window

0
 
chrisj1963Author Commented:
here is the code... with your addition...
<?php

$subject= "For decades, families in the Boston area held the secret of a Cape Cod vacation close to their hearts like a family treasure. Every Friday, scores of station wagons loaded with escapees from the city would file over the Bourne Bridge to the East Coast’s most enchanting vacation spot. Today, the word is out. While local Bostonians still make the trek for a Cape Cod vacation, many more people are crowding onto this coastal gem to take advantage of its amazing Atlantic views, superb seafood, and unique New England attitude. If you’re considering a Cape Cod vacation, here are some of the venues and attractions you should put on your list. ";
$pattern= "/(\w+\s){3}Cape Cod vacation(\s\w+){3}/";

preg_match($pattern, $subject, $matches);

foreach($matches as $val);
echo("... $val ...");
?>

Open in new window

0
 
Tyler LaczkoCommented:
Here's some revised code. The current issue is with the " "   (space)



<?php

$subject= "For decades, families in the Boston area held the secret of a Cape Cod vacation close to their hearts like a family treasure. Every Friday, scores of station wagons loaded with escapees from the city would file over the Bourne Bridge to the East Coast's most enchanting vacation spot. Today, the word is out. While local Bostonians still make the trek for a Cape Cod vacation, many more people are crowding onto this coastal gem to take advantage of its amazing Atlantic views, superb seafood, and unique New England attitude. If you're considering a Cape Cod vacation, here are some of the venues and attractions you should put on your list. ";
$pattern= "/(\w+\s){3}Cape(\s\w+){3}/";

preg_match_all($pattern, $subject, $matches);
$matches = $matches[0];

foreach($matches as $val)
      echo "... " . $val . " ...<br />";

?>
0
 
Tyler LaczkoCommented:
vacation is also lowercase

and its working except for the 3 words after.

code attached:
<?php

$subject= "For decades, families in the Boston area held the secret of a Cape Cod vacation close to their hearts like a family treasure. Every Friday, scores of station wagons loaded with escapees from the city would file over the Bourne Bridge to the East Coast's most enchanting vacation spot. Today, the word is out. While local Bostonians still make the trek for a Cape Cod vacation, many more people are crowding onto this coastal gem to take advantage of its amazing Atlantic views, superb seafood, and unique New England attitude. If you're considering a Cape Cod vacation, here are some of the venues and attractions you should put on your list. ";
$pattern= "/(\w+\s){3}Cape\sCod\svacation/";

preg_match_all($pattern, $subject, $matches);

//var_dump($matches);
$matches = $matches[0];

foreach($matches as $val)
	echo "... " . $val . " ...<br />";

?>

Open in new window

0
 
Tyler LaczkoCommented:
The other 2 are issues because there is a , after vacation.
0
 
chrisj1963Author Commented:
Hi again... I tried that and here is the result:

... secret of a Cape Cod vacation close ...

please see http://70.87.107.194/~g3crmco/aphrasetest4.php
which is using your exact code from your post...

the issues are that:
1) it is now only including 1 word (versus 3) on the right side of the phrase...
it should be
... secret of a Cape Cod vacation close to their ...

and
2) it is only generating one row of data when there should be 3

... secret of a Cape Cod vacation close to their ...
... trek for a Cape Cod vacation, many more people ...
... you’re considering a Cape Cod vacation, here are some ...

any other thoughts?
0
 
chrisj1963Author Commented:
I mistakenly accepted my own response as the solution, I want to award all points to:
professionalcomputersolutions with the highest possible scores in all categories. Please address this
0
 
chrisj1963Author Commented:
Hi - I am sorry I started responded before seing your followup posts.... thanks for your solution.!

there are a couple more to follow. Please look for them.  
1) How can we run the code to ignore case and punctuation.
2) The next after that will be how can we parse the results into a table:
eg.

Col 1  |  Col 2  | Col 3 | Col 4 | Col 5 | Col 6 | Col 7
secret | of | a | Cape Cod vacation | close | to | their
trek | for |  a | Cape Cod vacation | many | more | people
you’re | considering |  a | Cape Cod vacation | here | are | some
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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