Jump to content

Looking for information.. preg_replace


Scooby08

Recommended Posts

I basically have a function like so:

 

<?php
function AdjustForEdit($block)
{
$search[] = "/(['\"])?(templ\/)?images\//m"; 
$replace[] = "\\1".$SITEURL."/\\2images/";

$ret = preg_replace($search, $replace, $block);

return $ret;
}
?>

 

I was wondering if anybody could tell me what the numbers 1 and 2 in this line are doing exactly:

 

$replace[] = "\\1".$SITEURL."/\\2images/";

 

I can't seem to find any documentation on that either because I'm not too familiar with regex and do not know what exactly to search for.. Can anybody recommend any good resources??

 

 

Link to comment
Share on other sites

Thank you for the info guys..

 

I have read through some of the documentation and am having troubles figuring out how to do a  search for everything except specific words.. For example, I have this line:

 

/href=\"(.*)?\"/

 

which replaces all href's with whatever I like.. The thing I'm trying to figure out is how would I do the same, except not replace any href's like so:

 

<a href="javascript://">

 

I'm thinking something like this:

 

[^javascript\:\/\/]

 

but Im not sure how to incorporate that into this line:

 

/href=\"(.*)?\"/

 

Thanks again..

 

Link to comment
Share on other sites

/href=\"(.*)?\"/

 

A few things wrong here as is... you don't need to escape " characters in this case.. and avoid .* If you are going to use dot all matching, its generally safer to make it into a lazy quantifier. See this discussion to see why (read from reply 10 onwards). In this case, I would use a negated character class instead.

/href="[^"]+"/

 

The thing I'm trying to figure out is how would I do the same, except not replace any href's like so:

 

<a href="javascript://">

 

I'm thinking something like this:

 

[^javascript\:\/\/]

 

you are using a negated character class to tell the regex engine not to match either a j, or an a, or a v, etc.. what you need to understand is that a character class just represents one position in the string.. not a sequence such as a capture or non capturing set (...) or (?:...).

 

So if I understand you correctly, you want to replace any href content that is NOT javascript://? You can use negative look ahead assertions.

 

$arr = array('<a href="javascript://" id = "clueless_ID">', '<a href="whatever" class = "I_dunno">', '<a href="javascript://" id = "Rock_Me_Amadeus">');
foreach($arr as &$val){
    $val = preg_replace('#(<a.+?href=")(?!javascript://)[^"]+("[^>]*>)#', '\\1\\2', $val);
}
echo "<pre>".print_r($arr, true);

 

When you run the above script, you'll have to right-click to see the results... any href that has the content javascript:// doesn't get wiped out. Keep hacking away at the material you were reading.. in time, it really starts to fit together.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.