Link to home
Start Free TrialLog in
Avatar of Nura111
Nura111

asked on

Can anyone explain the following regex?

$str = preg_replace('/[\n\r\t]+/', '', $str);
ASKER CERTIFIED SOLUTION
Avatar of Derokorian
Derokorian
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nura111
Nura111

ASKER

Thank you!
HOw about:

'/\s{2,}/'

break it down part by part:

/ - opening delimiter
[ - group of characters
\n - new line
\r - carriage return
\t - tab character
] - close group
+ - one or more of characters matching any character in the group
/ - closing delimiter

HTH
'/\s{2,}/' Finds spaces in groups of 2 or more (ie: whenver there are two  spaces  between  a  word  instead of one like normal)
Avatar of Nura111

ASKER

Ok last question in the next phrase:
preg_replace('/(<a[^>]*>)(<img[^>]+alt=")([^"]*)("[^>]*>)(<\/a>)/i', '$1$3$5<br />', $buffer);

what is the '$1$3$5' pharse mean?

Well lets look at the regex first:

/(<a[^>]*>)(<img[^>]+alt=")([^"]*)("[^>]*>)(<\/a>)/i
/ Opening delimiter
( creates first match grouping
<a[^>]*> matches any anchor opening tag
) closes first grouping
( creates second grouping
<img[^>]+alt=" matches any img tag up to the alt attribute
) closes second grouping
( creates third grouping
[^"]* matches the characters found within the alt attribute
) closes third group
( creates fourth grouping
"[^>]*> matches the remainder of the img tag
) closes fourth grouping
( creates fifth grouping
<\/a> matches a closing anchor tag
) closes fifth grouping
/i makes all matches case insensitive

Then there is the replace with part:
$1$3$5<br />
dollar sign number is a back reference, refering the that's numbers equivalent group in the matching phrase. So...
$1 would be the opening anchor tag
$3 would be the alt attribute in the img tag
$5 would be the closing anchor tag
Avatar of Nura111

ASKER

geex you know regex!
Thank you!
Avatar of Nura111

ASKER

Im trying to add the following regex:
to replace a stylesheet and I get an error I cant see the syntax problem do you?
I can open a new question if you like

$buffer = preg_replace =('/<link[^>]+rel="[^"]*stylesheet"[^>]*/','<link href="mobile.css" rel="stylesheet" type="text/css"/>',$buffer)

Open in new window

What's the error?
Avatar of Nura111

ASKER

Parse error: syntax error, unexpected '='
haha didn't even notice...

$buffer = preg_replace =('/<link[^>]+rel="[^"]*stylesheet"[^>]*/','<link href="mobile.css" rel="stylesheet" type="text/css"/>',$buffer);

should be

$buffer = preg_replace('/<link[^>]+rel="[^"]*stylesheet"[^>]*/','<link href="mobile.css" rel="stylesheet" type="text/css"/>',$buffer);

preg_replace is a function call and should be followed by a (
Avatar of Nura111

ASKER

oh god whats wrong with ???sorry I dont know how i ddit notice that
Also for the results you are desiring you should use:

$buffer = preg_replace('/<link[^>]+rel="[^"]*stylesheet"[^>]*/','<link href="mobile.css" rel="stylesheet" type="text/css"/',$buffer);

Your's left an extra > after the link tag because you aren't matching for the final > but you are including a closing > in the replacement.
Avatar of Nura111

ASKER

What is the meaning of this sign alone ^ in a regex?
^ means not including so...
[^>] means all characters not including greater than.
Avatar of Nura111

ASKER

Im tryng to create a regex that will remove all the character between a certain div
for exmp:

<div id="banner">
remove
remove

</div>
Avatar of Nura111

ASKER

I tried:
$buffer = preg_replace('/(<div+id="banner".*.<\/div>/','', $buffer);

but its not working
Well first thing about preg_replace is that you can use your own delimiter, I like to use something that won't be present in my regx. Second you're not allowing for white space between <div and id= Try this: (also feel free to make another question, and I'll answer there if this doesn't work (cause I'm greedy for points! lol))

$banner = '#' // Opening delimiter
.'<div'   // opening of div
.'(.*)' // any character within the div tag
.'id="banner"' // with specific ID of banner
.'(.*)' // any character after id of the div and the closing div
.'</div>' // the closing div tag
.'#'; // closing delmiter
$buffer = preg_replace($banner,'', $buffer);

Open in new window


Note I only wrote it that way to separate out the parts of the regex. This is something I learned on EE from Ray_Paseur and its something I do now because it greatly improves how easy it is to understand what you are doing. Also Note: this will not work if there is a nested div in the banner div (because it will cut off at the first closing div).

HTH
$banner = '#'	// Opening delimiter
.'<div'		// opening of div
.'([^>]+)'	// any character within the div tag
.'id="banner"'	// with specific ID of banner
.'([^>]+)'	// any character after id of the div before the closing of the div tag
.'>'		// closing of the div tag
.'(.*)'		// any character within the DIV
.'</div>'	// closing of the  div
.'#';		// closing delmiter
$buffer = preg_replace($banner,'', $buffer);

Open in new window


Actually try that... so it doesn't match the first opening of any div until the last closing div... maybe...
Avatar of Nura111

ASKER

Can you write in in thus format please:$buffer = preg_replace('/(<div+id="banner".*.<\/div>/','', $buffer)
Avatar of Nura111

ASKER

I tried the first one its not working I have to add somehing for the spaces though,
here is the new question,
https://www.experts-exchange.com/questions/27412639/regex-ancor-a-text-between-divs.html

$buffer = preg_replace('#<div([^>]+)id="banner"([^>]+)>(.*)</div>#','', $buffer);

Open in new window

Avatar of Nura111

ASKER

ok find it:

$buffer = preg_replace('/<div([^>]+)id="banner">(.*?)<\/div>/s','test', $buffer); is working
not really sure how to explain it what is the diffrence between (.*?) and (.*)
Avatar of Nura111

ASKER

oh posted it in the wrong question sorry