News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Understanding preg_match_all

Started by Biology Forums, September 26, 2014, 09:25:06 AM

Previous topic - Next topic

Biology Forums

In Subs.php, I am trying to put all content that matches #<a href="(.*?)</a>#i (links) into an array. Then I'm cycling through the array of links called $urlz and if a link within the array contains WORD, it replaces it. But no matter what I do, it's not producing <span class="round5px midtext hidden">Hidden Link - <a style="font-weight: bold;" href="http://XYZ/index.php?action=login">Login</a> or <a style="font-weight: bold;" href="http://XYZ/index.php?action=join">Register</a></span>.

if(preg_match_all('#<a href="(.*?)</a>#i', $message, $urlz, PREG_SET_ORDER))
{
// Cycle through each URL found
foreach($urlz as $urlz)
{
if(strpos($urlz, 'WORD') == false)
$urlz = '<span class="round5px midtext hidden">Hidden Link - <a style="font-weight: bold;" href="http://XYZ/index.php?action=login">Login</a> or <a style="font-weight: bold;" href="http://XYZ/index.php?action=join">Register</a></span>';
}

}

margarett

* margarett has absolutely no idea about preg_match :P

But for debug, you should probably set the result of preg_match_all to a variable and then print_r it (so that you can see what's being produced)

Then, I don't think you can do foreach($urlz as $urlz). The arguments need to have different names, like foreach($urlz as $url)
Then use $url in the "if" below. And since you want to write to the item in $url, it should be foreach($urlz as &$url) (so that you're passing the reference to the variable and, as such, you can change it inside the foreach)
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Arantor

Because you break it with the loop.

foreach ($urlz as $urlz) will overwrite the array of items with the first item on the first iteration. You could make a reference out of it with foreach ($urlz as &$url) but this only changes $urlz, it won't change your original string. For that you will need to do a further replace (probably at that point a str_replace, because you already have the string that's part of the original text)

I'd wonder why you can't use the mod that already does this for you, though...

Biology Forums

I had an inkling the problem was with the damn REGEX used. I'll use print_r to see if it's actually getting anything.

@Arantor... Thanks, are you referring to SMFHack's? It's because his isn't selective. It replaces all links. I want this to go through different IF and replace them individually as per the IF statements I make.

For exampe, if WORD, replace with A, if BIRD, replace with B.

Arantor

To your first point, of course it's getting something, if it wasn't the rest of it wouldn't be touched.

Your problem, as stated by two different people, is that you're corrupting $urlz with your foreach. But yes, do a print_r on it so you can see what you get.

Being selective... I could see that being problematic actually.

Biology Forums

Let's pretend in a post message you have an internal link and an external link. By being unselective, both links would be hidden. A webmaster wants the user to stay longer, so why hide the internal link? That's my logical behind it. By implementing this, I can check if the URL possesses my domain name.

Why would it be problematic from your perspective?

Biology Forums

Here's a working version of what I have thus far.

$notice = '<span class="round5px midtext hidden">Hidden Link - <a style="font-weight: bold;" href="http://biology-forums.com/index.php?action=login">Login</a> or <a style="font-weight: bold;" href="http://biology-forums.com/index.php?action=register">Register</a></span>';

if(preg_match_all('#<a href="(.*?)</a>#i', $message, $urlz, PREG_SET_ORDER))
{
// Cycle through each image
foreach($urlz as $b)
{
if(strpos($b[0], 'biology-forums.com') !== false || strpos($b[0], 'youtube.com') !== false || strpos($b[0], 'homeworkclinic') !== false)
echo '';
else
$message = str_replace($b[0], $notice, $message); // Replace the link in the message
}
}


Would anyone happen to know the Regex expression that excludes images embedded in <a href""></a>?

Advertisement: