Solved

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

Posted on 2013-06-02
8
279 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
[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
8 Comments
 
LVL 15

Expert Comment

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

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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
LVL 110

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
 
LVL 58

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 58

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 look for a specific file type in a local or remote server directory using PHP.

623 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