Solved

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

Posted on 2013-06-02
8
278 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 57

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 57

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 57

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
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 …

734 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