Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

preg_match_all: begins with index.php ends with " or '

Posted on 2010-01-11
8
Medium Priority
?
609 Views
Last Modified: 2012-06-27
I want to put all matching URIs in an array.

Must begin with index.php and end with " or '

The " or ' should not go in the array, just the URI.
<?php

$html = '

asfdsfdsfadsfadsf
<a class="cartlink" href="index.php?option=com_login" title="Login">Login</a> 

adsfdsaf

<a href="http://www.mysite.com/index.php?option=com_hello">Test</a> 

adsfdsaf

<a href="http://www.google.com">Test</a> 

';


$pat='/^(index)*(")|(\')/'; 

preg_match_all($pat, $html, $oLinks); 

var_dump($oLinks);

?>


Should return these in an array:
     index.php?option=com_login
     index.php?option=com_hello

Open in new window

0
Comment
Question by:hankknight
8 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26283085
Where are the URIs coming from?  Can you please post some test data?  Thanks, ~Ray
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26283097
Amazing!  EE just produced the test data right after I asked for it.  Ha!  Love those over-engineered applications.
0
 
LVL 2

Expert Comment

by:xpert13
ID: 26283182
$pat='/index\.php\?[^\"]+/';
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 total points
ID: 26283186
Give this a try... Best regards, ~Ray
<?php // RAY_temp_hanknight.php
error_reporting(E_ALL);
echo "<pre>\n";

$html = <<<EOHTML

asfdsfdsfadsfadsf
<a class="cartlink" href="index.php?option=com_login" title="Login">Login</a>

adsfdsaf

<a href="http://www.mysite.com/index.php?option=com_hello">Test</a>
<a href='http://www.mysite.com/index.php?option=terminated_by_apostrophe'>Test</a>

adsfdsaf

<a href="http://www.google.com">Test</a>

EOHTML;


$pat='/index.+"|index.+\'/';

preg_match_all($pat, $html, $oLinks);

var_dump($oLinks);

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26283222
Or if you wanted to reject index.html because it did not contain ".php"
<?php // RAY_temp_hanknight.php
error_reporting(E_ALL);
echo "<pre>\n";

$html = <<<EOHTML

asfdsfdsfadsfadsf
<a class="cartlink" href="index.php?option=com_login" title="Login">Login</a>

adsfdsaf

<a href="http://www.mysite.com/index.php?option=com_hello">Test</a>
<a href='http://www.mysite.com/index.php?option=terminated_by_apostrophe'>Test</a>

adsfdsaf
<a href="http://www.mysite.com/index.html?option=rejected_not_php">Test</a>
<a href="http://www.google.com">Test</a>

EOHTML;


$pat='/index\.php.+"|index\.php.+\'/';

preg_match_all($pat, $html, $oLinks);

var_dump($oLinks);

Open in new window

0
 
LVL 2

Accepted Solution

by:
xpert13 earned 1200 total points
ID: 26283230
This version of my pattern is better:

$pat='/index\.php\?[^\"^\']+/';

Open in new window

0
 
LVL 27

Assisted Solution

by:ddrudik
ddrudik earned 400 total points
ID: 26283287
Consider using a pattern that avoids overmatching outside of the HREF tag or outside of the "option" variable etc.:
<?php
$sourcestring="your source string";
preg_match_all('/<a[^>]*href=([\'"])(?:(?!\1).)*(index\.php\?option=(?:(?!\1|&).)*)\1/is',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
 
$matches Array:
(
    [0] => Array
        (
            [0] => <a class="cartlink" href="index.php?option=com_login"
            [1] => <a href='http://www.mysite.com/index.php?option=com_hello'
        )

    [1] => Array
        (
            [0] => "
            [1] => '
        )

    [2] => Array
        (
            [0] => index.php?option=com_login
            [1] => index.php?option=com_hello
        )

)

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 26283309
Thank you both.  xpert13's last post works the best.   The others put in extra stuff.

Thanks!
<?php

$html = '
asfdsfdsfadsfadsf
<a class="cartlink" href="index.php?option=com_login" title="Login">Login</a> 
adsfdsaf
<a href="http://www.mysite.com/index.php?option=com_hello">Test</a> 
asdfadsf
<a href=\'http://www.mysite.com/index.php?option=com_hello\'>Test</a> 
asdf
<a href=\'index.php?option=com_hello1\'>Test</a> 
adsfdasf
<a href="index.php?option=com_hello2">Test</a> 
adsfdasf
<a href="/index.php?option=com_hello3">Test</a> 
adsfdsaf
<a href="http://www.google.com">Test</a> 
';

$pat='/index.+"|index.+\'/'; 
preg_match_all($pat, $html, $oLinks); 
foreach ($oLinks[0] as $oLink) echo htmlentities($oLink). '<br />';

echo '<hr />';

$pat='/index\.php\?[^\"]+/';
preg_match_all($pat, $html, $oLinks); 
foreach ($oLinks[0] as $oLink) echo htmlentities($oLink). '<br />';

echo '<hr />';

$pat='/index\.php\?[^\"^\']+/'; 
preg_match_all($pat, $html, $oLinks); 
foreach ($oLinks[0] as $oLink) echo ($oLink). '<br />';

?>

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and 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…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
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.
Suggested Courses

571 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