Searching out parts of a long variable content in php

Glenn

Member
If I have a lot of content stored in a variable, how do I find each occurrence of a certain word. For example, I want to find the word grand and then grab the word that follows it.

Basically, I want to check each time the word grand is used then save the following word in my database.

Let's say I set it up like this.

$story = "The princess went to the grand ball by herself. She was waring a yellow dress and grand shoes. While there, she dance with lots of men but one was a grand prince. He lived another village in a grand castle."

So, I need to run some sort of a for loop that is going to pick out the words "ball", "shoes", "prince", and "castle".

How can I do this?
 

CaldwellYSR

Member
I would explode the string into an array and then loop through the array searching for the word you want. For example:

PHP:
$story = "The princess went to the grand ball by herself. She was waring a yellow dress and grand shoes. While there, she dance with lots of men but one was a grand prince. He lived another village in a grand castle."

$pieces = explode(" ", $story); // First parameter is a space " " not ""
for($i = 0; $i < sizeof($pieces); $i++) {
    
    if($pieces[$i] === "grand") {

        // Put $pieces[$i + 1] into database

    }

}

Be aware that if grand is the last word then you'll get an error with this code (I don't know the php version but in Java it would be IndexOutOfBoundsException). Basically it would try to grab the NEXT word and there would be no word there to grab. So you should either wrap the database insertion in a try catch clause (if php has try catch?) or use another if statement to make sure there is indeed another word to put in there.
 
Last edited:

Glenn

Member
maybe I need to just say exactly what I want instead of an example like that. I need to have the code of some web pages, which I already can get, and find each time a link to the page somesite.com/this/##### where the "#####" is different each time. I need to save all of the different "#####" to my database. Of coarse the code would be something like <a href="this/B126P">

So, basically I would need to look for all occurrences of "<a href=\"this/" and grab the next 5 characters.
 
Last edited:

CaldwellYSR

Member
Now that is a different beast entirely and I have no help to give there. I'm not really a php person, just kind of know the syntax ;)
 

Glenn

Member
I see you are from Sewanee. I'm not too far from there. I live in Fayetteville. I sometimes go through Sewanee to get to Chattanooga or Knoxville.
 

CaldwellYSR

Member
I see you are from Sewanee. I'm not too far from there. I live in Fayetteville. I sometimes go through Sewanee to get to Chattanooga or Knoxville.

I should change that... Up until this semester I've been going to school at Sewanee but I live in Shelbyville. I've got some friends in Fayetteville, haven't been there in a while though.
 

Glenn

Member
Been to Shelbyville many times. I am from Mt Juliet so I have been there some for ball games. I go through Shelbyville when I go to Murfreesboro.
 

CaldwellYSR

Member
Been to Shelbyville many times. I am from Mt Juliet so I have been there some for ball games. I go through Shelbyville when I go to Murfreesboro.

Yeah Shelbyville is only good for Walking Horses and getting the Murfreesboro ;) There's nothing in the bodunk town. I'm ready to get out
 

Glenn

Member
Here's what I have.

// $i is for the node to go to all pages and grab links
for($i=1; $i<=25; $i++) {

$categoryPageUrl = "http://astore.amazon.com/luv2kayak-20?node=" . $node . "&page=" . $i;

// $x is a counter to get each of the 10 item numbers on a page
for($x=1; $x<=10; $x++) {

$html = file_get_contents($categoryPageUrl);
$start = strpos($html, "luv2kayak-20/detail") + 20;
$end = strpos($html, "luv2kayak-20/detail") + 30;
$itemNum = substr($html, $start, $end-$start);

echo $itemNum, "<br>";
}
}

The output here gives me the same characters for the $itemNum even though there are 10 on each page. How do I get it to move to the next one each time it reads what I am telling it to?
 

Glenn

Member
Just changed, but still doing the same thing.

for($i=1; $i<=25; $i++) {

$categoryPageUrl = "http://astore.amazon.com/luv2kayak-20?node=" . $node . "&page=" . $i;
$html = file_get_contents($categoryPageUrl);


// $x is a counter to get each of the 10 item numbers on a page
for($x=1; $x<=10; $x++) {

$start = strpos($html, "luv2kayak-20/detail") + 20;
$end = strpos($html, "luv2kayak-20/detail") + 30;
$itemNum = substr($html, $start, $end-$start);

echo $itemNum, "<br>";
 

CaldwellYSR

Member
strpos returns the FIRST instance of the string you're looking for... That's why it doesn't work :p I still don't know how to make it work though ;) sorry bro

PS: Wrap your code in php tags (PHP)(/PHP) where the ()'s are really []'s
 

CaldwellYSR

Member
Whew Glenn you're reaching back there, I don't know if my memory will go back that far XD

If you managed to get this working correctly where you're grabbing the URL's and putting them in a database then can't you just echo them out of that database into the src of and img tag?
 

Glenn

Member
Whew Glenn you're reaching back there, I don't know if my memory will go back that far XD

If you managed to get this working correctly where you're grabbing the URL's and putting them in a database then can't you just echo them out of that database into the src of and img tag?

Yes, what I created has worked out great. I use it every hour of every day with a cron setting.

I need to copy them for what I am using them for right now. About 2,000 products and an average of about 3 pics each.
 

benjamin.morgan

New Member
I would explode the string into an array and then loop through the array searching for the word you want. For example:

PHP:
$story = "The princess went to the grand ball by herself. She was waring a yellow dress and grand shoes. While there, she dance with lots of men but one was a grand prince. He lived another village in a grand castle."

$pieces = explode(" ", $story); // First parameter is a space " " not ""
for($i = 0; $i < sizeof($pieces); $i++) {
    
    if($pieces[$i] === "grand") {

        // Put $pieces[$i + 1] into database

    }

}

Be aware that if grand is the last word then you'll get an error with this code (I don't know the php version but in Java it would be IndexOutOfBoundsException). Basically it would try to grab the NEXT word and there would be no word there to grab. So you should either wrap the database insertion in a try catch clause (if php has try catch?) or use another if statement to make sure there is indeed another word to put in there.


Not sure if this would work but you could try. I assume they are searching for something through an input type text. if so you could do this.

HTML:
<input type="text" name="searchfield">

PHP:
$story = "The princess went to the grand ball by herself. She was waring a yellow dress and grand shoes. While there, she dance with lots of men but one was a grand prince. He lived another village in a grand castle."

$pieces = explode(" ", $story); 
for($i = 0; $i < sizeof($pieces); $i++) {
    
    if($pieces[$i] === $_POST['searchfield']) { // changed grand to $_POST['searchfield']
        // Put $pieces[$i + 1] into database

    }

}
 

CaldwellYSR

Member
Yes, what I created has worked out great. I use it every hour of every day with a cron setting.

I need to copy them for what I am using them for right now. About 2,000 products and an average of about 3 pics each.

Well I still don't see the problem. Why can't you just pull the url's from the database and echo them into the src attribute?
 

CaldwellYSR

Member
Let's just say I need to download more than pictures.

That doesn't sound shady at all.....

If you have the code to put substance X into your database then it should be no problem to echo substance X out of that database and into your web page...
 

Glenn

Member
That doesn't sound shady at all.....

If you have the code to put substance X into your database then it should be no problem to echo substance X out of that database and into your web page...

It sounded a little off after I went back and read it but it is legal and ethical.

If they change the name or location of anything, my site is messed up.
 

CaldwellYSR

Member
It sounded a little off after I went back and read it but it is legal and ethical.

If they change the name or location of anything, my site is messed up.

That's true but that's really the case with any site that relies on pulling from other websites. What are you trying to do? There might be a better way
 
Top