Searching out parts of a long variable content in php

Discussion in 'Web Design' started by Glenn, Mar 1, 2012.

to remove all ads.
  1. Glenn

    Glenn Member

    Messages:
    189
    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?
     
  2. CaldwellYSR

    CaldwellYSR Member

    Messages:
    658
    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: Mar 1, 2012
  3. Glenn

    Glenn Member

    Messages:
    189
    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: Mar 1, 2012
  4. CaldwellYSR

    CaldwellYSR Member

    Messages:
    658
    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 ;)
     
  5. Glenn

    Glenn Member

    Messages:
    189
    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.
     
  6. CaldwellYSR

    CaldwellYSR Member

    Messages:
    658
    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.
     
  7. Glenn

    Glenn Member

    Messages:
    189
    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.
     
  8. CaldwellYSR

    CaldwellYSR Member

    Messages:
    658
    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
     
  9. Glenn

    Glenn Member

    Messages:
    189
    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?
     
  10. Glenn

    Glenn Member

    Messages:
    189
    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>";
     
  11. CaldwellYSR

    CaldwellYSR Member

    Messages:
    658
    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
     
  12. Glenn

    Glenn Member

    Messages:
    189
    How would I use this to copy an image from that site to my site?
     
  13. CaldwellYSR

    CaldwellYSR Member

    Messages:
    658
    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?
     
  14. Glenn

    Glenn Member

    Messages:
    189
    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.
     
  15. benjamin.morgan

    benjamin.morgan New Member

    Messages:
    269

    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

        
    }

    }

     
  16. CaldwellYSR

    CaldwellYSR Member

    Messages:
    658
    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?
     
  17. Glenn

    Glenn Member

    Messages:
    189
    Let's just say I need to download more than pictures.
     
  18. CaldwellYSR

    CaldwellYSR Member

    Messages:
    658
    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...
     
  19. Glenn

    Glenn Member

    Messages:
    189
    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.
     
  20. CaldwellYSR

    CaldwellYSR Member

    Messages:
    658
    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
     

Share This Page