Solved

create a php pattern to extract many lines of keywords in a block of text

Posted on 2013-06-02
8
266 Views
Last Modified: 2013-06-04
{'id':'139844','showcode':'Click to copy','copy':'want-this', 'url':
words here
{'id':'139844','showcode':'Click to copy','copy':'want-this', 'url':

{'id':'13984411111111111','showcode':'Click to copy','copy':'want-this', 'url':

{'id':'139844','showcode':'Click to copy','copy':'want-this', 'url':
{'id':'139844','showcode':'Click to copy','copy':'want-this', 'url':


in a block of text want many lines of 'want-this'

text
want-this
may be different

but is always surrounded by
','showcode':'Click to copy','copy':'
', 'url':
0
Comment
Question by:rgb192
8 Comments
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39214171
How do you generate the above text block??
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 39214476
Are you looking for something like this

$line = "{'id':'139844','showcode':'Click to copy','copy':'want-this', 'url':";
$pattern = "/^{'id':'[0-9]+','showcode':'Click to copy','copy':'(.*)', 'url':.*$/";
$matches = array();
$result = preg_match($pattern, $line, $matches);
echo "<pre>"';
print_r($matches);
echo "</pre>"';

Open in new window


The text you want should be in $matches[1];
0
 

Author Comment

by:rgb192
ID: 39214557
by: julianHPosted on 2013-06-02 at 11:35:24ID: 39214476

works for

$line = "{'id':'139844','showcode':'Click to copy','copy':'want-this', 'url':";

but want many lines

$line="
{'id':'139844','showcode':'Click to copy','copy':'want-this', 'url':
words here
{'id':'139844','showcode':'Click to copy','copy':'want-this', 'url':

{'id':'13984411111111111','showcode':'Click to copy','copy':'want-this', 'url':

{'id':'139844','showcode':'Click to copy','copy':'want-this', 'url':
{'id':'139844','showcode':'Click to copy','copy':'want-this', 'url':
";


How do you generate the above text block??

code is html source code
0
 
LVL 108

Assisted Solution

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

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo '<pre>';

/* PROBLEM DEFINITION
 *
 * SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28145196.html
 *
 */

// LEADING SIGNAL STRING
$head = <<<EOD
','showcode':'Click to copy','copy':'
EOD;

// TRAILING SIGNAL STRING
$tail = <<<EOD
', 'url':
EOD;

// TEST DATA POSTED WITH THE QUESTION
$data = <<<EOD
{'id':'139844','showcode':'Click to copy','copy':'want-this', 'url':
words here
{'id':'139844','showcode':'Click to copy','copy':'want-this', 'url':

{'id':'13984411111111111','showcode':'Click to copy','copy':'want-this', 'url':

{'id':'139844','showcode':'Click to copy','copy':'want-this', 'url':
{'id':'139844','showcode':'Click to copy','copy':'want-this', 'url':
EOD;

// MAKE THE STRING INTO AN ARRAY OF LINES
$arr = explode(PHP_EOL, $data);

// PROCESS LINE-BY-LINE
foreach ($arr as $str)
{
    $str = trim($str);

    // LOOK FOR THE SIGNAL STRINGS
    if ( (strpos($str, $head) === FALSE) && (strpos($str, $tail) === FALSE) )
    {
        echo PHP_EOL . "NOTHING FOUND IN $str";
    }
    else
    {
        // USING SIGNAL STRINGS, ISOLATE THE DESIRED DATA
        $sub = explode($head, $str);
        $sub = explode($tail, $sub[1]);
        $sub = $sub[0];
        echo PHP_EOL . "FOUND <b>$sub</b> IN $str";
    }
}

Open in new window

Best regards, ~Ray
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 51

Accepted Solution

by:
Julian Hansen earned 250 total points
ID: 39215438
Then just adapt to run multiple lines through the preg_match
<?php
date_default_timezone_set('Africa/Johannesburg');

$data = <<<DATA
{'id':'139844','showcode':'Click to copy','copy':'want-this', 'url':
words here
{'id':'139844','showcode':'Click to copy','copy':'want-this', 'url':

{'id':'13984411111111111','showcode':'Click to copy','copy':'want-this', 'url':

{'id':'139844','showcode':'Click to copy','copy':'want-this', 'url':
{'id':'139844','showcode':'Click to copy','copy':'want-this', 'url':
DATA;
$pattern = "/{'id':'[0-9]+','showcode':'Click to copy','copy':'(.*)?', 'url':.*/";
$matches = array();
$lines = explode("\n", $data);
foreach($lines as $l) {
	$result = preg_match($pattern, $l, $matches);
	echo "<pre>";
	print_r($matches);	
	echo "</pre>";
}

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 39218017
both codes get 'want this'

thanks
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 39218156
You are welcome - thanks for the points.
0
 

Author Comment

by:rgb192
ID: 39220765
Thank you
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

760 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

17 Experts available now in Live!

Get 1:1 Help Now