Solved

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

Posted on 2013-06-16
7
264 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
  • 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 108

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now