Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
280 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 59

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 59

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 59

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

722 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