Solved

many rows of html text, want a value from each line

Posted on 2013-06-16
7
272 Views
Last Modified: 2013-07-09
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here

many lines
please tell me how to get
want-this-number
0
Comment
Question by:rgb192
[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
  • 4
  • 2
7 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39252118
For the exact text provided, this should work:
preg_match_all('#(?<=<a href="/start\.php\?a=request-link&amp;s=new-york&amp;f=intake&amp;l=)\d+#',$text,$matches);
print_r($matches);

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39252120
You could also be more flexible in the URLs you accept, such as:

preg_match_all('#<a href="/start\.php[^"]*(?:\?|&amp;)l=(\d+)#',$text,$matches);
print_r($matches[1]);

Open in new window

0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39252957
Please see http://www.laprbass.com/RAY_temp_rgb192.php

The important part of this question is not so much the part you want to extract, but instead it is the set of rules that describe the surrounding data.  In this case the rules can be inferred from the test data sample, and the test data sample shows exactly the same surrounding data for each element you want.  Maybe that's good enough, but it might be a more interesting and useful question if you got some of the live data and used that for your test set.  Just a thought... ~Ray

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28158846.html

$txt = <<<EOD
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
EOD;


// START SIGNAL
$a = 'l=';

// STOP SIGNAL
$z = '"';

// MAKE AN ARRAY BY BREAKING STRING ON START SIGNAL
$arr = explode($a, $txt);
unset($arr[0]);

// USE THE STOP SIGNAL TO DECLOP THE ELEMENTS OF THE ARRAY
foreach ($arr as $sub)
{
    $sub = explode($z, $sub);
    $out[] = $sub[0];
}

// SHOW THE EXTRACTED SUBSTRINGS
print_r($out);

Open in new window

0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:rgb192
ID: 39259997
Terry's code
Array ( ) Array ( [0] => Array ( ) )


<?php

$text = <<<EOD
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
EOD;
  preg_match_all('#<a href="/start\.php[^"]*(?:\?|&amp;)l=(\d+)#',$text,$matches);
print_r($matches[1]);


preg_match_all('#(?<=<a href="/start\.php\?a=request-link&amp;s=new-york&amp;f=intake&amp;l=)\d+#',$text,$matches);
print_r($matches);

Open in new window

0
 

Author Comment

by:rgb192
ID: 39260003
Ray has the correct answer, not sure if Terry has correct answer

I opened another question asking why Ray's code works
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28161784.html
0
 

Author Comment

by:rgb192
ID: 39274903
did I implement Terry's code correctly?
0
 

Author Closing Comment

by:rgb192
ID: 39313192
Ray had the only answer that I knew how to use.

Thanks
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!

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

730 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