How do I get the value between these strings?

Discussion in 'PHP' started by Imozeb, Mar 21, 2010.

  1. #1
    I created a function to loop through the values in a paragraph and search for specific values (the values between the div tags). But I can't seem to get it to work. Can someone help me?

    Here is my code.

    PHP code:
    //pre set vars
    $uppervar = 10;
    $lowervar = 1;
    				$intpos = 0;
    				$finalpos = 0;
    				$num = 0;
    				$finaldata = "";
    				//get data between limits
    				while($num < $upperval)
    				{
    					$num += 1;
    //get var name
    					$varname = 'width:740px; margin-left:auto; margin-right:auto; text-align:left;" id="comment_' . $num . '_'><b><a style="color: #347C17;" id="comment_' . $num . '_';
    //get position of 38 bytes behind $varname (should be beginning of div tag)
    					$intpos = (strpos($data, $varname) - 38);
    					//append data if higher than lowerval
    					if($num >= $lowerval)
    					{
    //get position of 6 bytes after </div> (should right after closing div tag)
    						$finalpos = (strpos($data, '</div>', $intpos) + 6);
    						$adata  = substr($data, $intpos, $finalpos);
    //append data so $finaldata should be divs 1-10 by the end of the loop
    						$finaldata .= $adata;
    					}
    				}
    				//echo data
    				echo("$finaldata");
    			}
    Code (markup):
    $data value:
    	<div style="background-color:#333333; width:740px; margin-left:auto; margin-right:auto; text-align:left;"><b><a style="color: #347C17;" id="comment_1_">test1</a></b></div>
    
    		<div style="background-color:#333333; width:740px; margin-left:auto; margin-right:auto; text-align:left;"><b><a style="color: #347C17;" id="comment_2_">test2</a></b></div>
    													<div style="background-color:#CCCCCC; width:740px; margin-left:auto; margin-right:auto; text-align:left;"><b><a style="color: #347C17;" id="comment_3_">test3</a></b></div>
    													<div style="background-color:#333333; width:740px; margin-left:auto; margin-right:auto; text-align:left;"><b><a style="color: #347C17;" id="comment_4_">test4</a></b></div>
    													<div style="background-color:#CCCCCC; width:740px; margin-left:auto; margin-right:auto; text-align:left;"><b><a style="color: #347C17;" id="comment_5_">test5</a></b></div>
    													<div style="background-color:#333333; width:740px; margin-left:auto; margin-right:auto; text-align:left;"><b><a style="color: #347C17;" id="comment_6_">test6</a></b></div>
    													<div style="background-color:#CCCCCC; width:740px; margin-left:auto; margin-right:auto; text-align:left;"><b><a style="color: #347C17;" id="comment_7_">test7</a></b></div>
    													<div style="background-color:#CCCCCC; width:740px; margin-left:auto; margin-right:auto; text-align:left;"><b><a style="color: #347C17;" id="comment_8_">test8</a></b></div>
    													<div style="background-color:#333333; width:740px; margin-left:auto; margin-right:auto; text-align:left;"><b><a style="color: #347C17;" id="comment_9_">test9</a></b> </div>
    													<div style="background-color:#CCCCCC; width:740px; margin-left:auto; margin-right:auto; text-align:left;"><b><a style="color: #347C17;" id="comment_10_">test10</a></b></div>
    													<div style="background-color:#333333; width:740px; margin-left:auto; margin-right:auto; text-align:left;"><b><a style="color: #347C17;" id="comment_11_">test11</a></b></div>
    Code (markup):
    Thanks to whoever can help me.

    ~imozeb :)
     
    Last edited: Mar 21, 2010
    Imozeb, Mar 21, 2010 IP
  2. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The if statements are correct I think but the strpos and substr commands are mestup...
     
    Imozeb, Mar 22, 2010 IP
  3. skywebsol

    skywebsol Well-Known Member

    Messages:
    161
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    you should use the cURl to get the all HTML of your page then find the HTML tags
     
    skywebsol, Mar 22, 2010 IP
  4. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That is all the html of the page. I get the $data data via f_read(). That works because if I don't have the code their it outputs all of the divs, but I only want the data between the upper and lower limits. How would I find the tags. I tried strpos to find the start of the tag and strpos again with an offset of 5 to find the end of the tag. Then I tried to use substr to cut just the data between the open and close div tags. It should work, but I keep getting crazy values.

    Please help me!!!

    ~imozeb
     
    Imozeb, Mar 22, 2010 IP
  5. skywebsol

    skywebsol Well-Known Member

    Messages:
    161
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #5
    skywebsol, Mar 22, 2010 IP
  6. skywebsol

    skywebsol Well-Known Member

    Messages:
    161
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #6
    i think this will help you

     
    skywebsol, Mar 22, 2010 IP
  7. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks. I didn't use curl but I did see what you did with substr. ($end-$start) I checked the PHP site and I found out that substr's last value is the length not the last value!!! OMG I gave myself so much frustration for nothing! LOL :)
     
    Imozeb, Mar 22, 2010 IP