Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-06-02
8
Medium Priority
?
282 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 60

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 1000 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 60

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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 …
Suggested Courses

927 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