Solved

cURL search

Posted on 2013-01-16
8
176 Views
Last Modified: 2013-01-16
Hi experts,

I need to use this for more fields with same name and don't know how to search for them.. Please help.

if (substr($str, strpos($str, 'some text')) !== false) {
$string=substr($str, strpos($str, 'fieldname')+10,20);
$string=substr($string, strpos($string, 'blah')+4,20);
$string=substr($string, strpos($string, '>')+1,100);
$string=substr($string, 0, strpos($string, '<'));
}

Thanks.
0
Comment
Question by:jebbie635
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38781780
Can you be little more clear on the scenario, how you are searching..You said curl in heading where is the curl code and how you are searching
0
 

Author Comment

by:jebbie635
ID: 38781822
I guess it's just php. The first part of my script is curl, sorry for that, am newbie. Please tell me how to search for the fields. This is what i want to do:

first search the source code of a page for 'some text', if found continue to search for labels named the same and then get text if label is found. This code is just for the first label which i already know it's there because it found 'some text'.

if (substr($str, strpos($str, 'some text')) !== false) {
$string=substr($str, strpos($str, 'labelname')+10,20);
$string=substr($string, strpos($string, 'blah')+4,20);
$string=substr($string, strpos($string, '>')+1,100);
$string=substr($string, 0, strpos($string, '<'));
}

How do i search for the next label which has the same name as first one? I need to do this for 20 labels.
0
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38782230
read the page and programactically find the list of labels in that page

Then a call a function with required parameters like label,serach term, etc..and return data from that function

Hope its clear
0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 35

Expert Comment

by:gr8gonzo
ID: 38782320
Use the Simple HTML DOM library to parse the page - it's going to be far easier and more flexible. I wrote an article on how to use it to manipulate HTML pages, although you can use the same article to simply extract information from a page after you download it with cURL:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_10277-HTML-Manipulation-Made-Easy.html
0
 
LVL 35

Assisted Solution

by:gr8gonzo
gr8gonzo earned 500 total points
ID: 38782346
The basics are:

// Include the library
require("simple_html_dom.php");

// Assuming $str contains all the HTML from your cURL request
if (strpos($str, 'some text') !== false)
{
   // Parse the HTML
   $html = str_get_html($str);

   // Loop through all <blah> elements
   foreach($html->find("blah") as $blah)
   {
      // Show the contents of that blah tag
      echo "Outertext = " . $blah->outertext . "\n";
      echo "Innertext = " . $blah->innertext . "\n";
      echo "Plaintext = " . $blah->plaintext . "\n";
   }
}
0
 

Author Comment

by:jebbie635
ID: 38783771
$str=post($url, $referer, $agent, $cookie, $vars);

I need to search for this

<div class="someClass">someText</div>

because i have also other <div class="someClass"> with someOtherText which i don't need, and then get the link. I have to do this for 40 possible links.

$link=substr($str, strpos($str, 'someClass')+11,20);
$link=substr($link, strpos($link, 'someText')+14,100);
$link=substr($link, strpos($link, '<a href="')+9,1000);
$link=substr($link, 0, strpos($link, '>'));

Open in new window

This is what i have so far:

$link1=substr($str, strpos($str, 'someClass')+19,20);
$link1=substr($link1, strpos($link1, 'someText')+9,100);
$link1=substr($link1, strpos($link1, '<a href="')+9,1000);
$link1=substr($link1, 0, strpos($link1, '>'));

$link2=substr($str, strpos($str, 'someClass')+19,1000);
$link2=substr($link2, strpos($link2, 'someClass')+9,100);
$link2=substr($link2, strpos($link2, 'someText')+9,100);
$link2=substr($link2, strpos($link2, '<a href="')+9,1000);
$link2=substr($link2, 0, strpos($link2, '>'));
if ($link2) {
some action
}
...
$link40=substr($str, strpos($str, 'someClass')+19,40000);
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
...
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
$link40=substr($link40, strpos($link40, 'someText')+9,100);
$link40=substr($link40, strpos($link40, '<a href="')+9,1000);
$link40=substr($link40, 0, strpos($link40, '>'));
if ($link40) {
some action
}

Open in new window

Can you simplify the search of 'someClass'? This is a lot of code.
0
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 38784792
Try my previous code, but replace this line:

foreach($html->find("blah") as $blah)

with:

foreach($html->find("div.someClass") as $div)

Then you can check the value of $div->plaintext and compare it to "someText" or whatever you're looking for.
0
 

Author Closing Comment

by:jebbie635
ID: 38784959
It works. Thanks.
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question