?
Solved

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

Posted on 2010-01-11
8
Medium Priority
?
597 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
[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 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
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 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

Technology Partners: 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!

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…
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.
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 look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

801 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