Nura111
asked on
Can anyone explain the following regex?
$str = preg_replace('/[\n\r\t]+/' , '', $str);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
/ - 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)
ASKER
Ok last question in the next phrase:
preg_replace('/(<a[^>]*>)( <img[^>]+a lt=")([^"] *)("[^>]*> )(<\/a>)/i ', '$1$3$5<br />', $buffer);
what is the '$1$3$5' pharse mean?
preg_replace('/(<a[^>]*>)(
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
/(<a[^>]*>)(<img[^>]+alt="
/ 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
ASKER
geex you know regex!
Thank you!
Thank you!
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
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)
What's the error?
ASKER
Parse error: syntax error, unexpected '='
haha didn't even notice...
$buffer = preg_replace =('/<link[^>]+rel="[^"]*st ylesheet"[ ^>]*/','<l ink href="mobile.css" rel="stylesheet" type="text/css"/>',$buffer );
should be
$buffer = preg_replace('/<link[^>]+r el="[^"]*s tylesheet" [^>]*/','< link href="mobile.css" rel="stylesheet" type="text/css"/>',$buffer );
preg_replace is a function call and should be followed by a (
$buffer = preg_replace =('/<link[^>]+rel="[^"]*st
should be
$buffer = preg_replace('/<link[^>]+r
preg_replace is a function call and should be followed by a (
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[^>]+r el="[^"]*s tylesheet" [^>]*/','< 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.
$buffer = preg_replace('/<link[^>]+r
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.
ASKER
What is the meaning of this sign alone ^ in a regex?
^ means not including so...
[^>] means all characters not including greater than.
[^>] means all characters not including greater than.
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>
for exmp:
<div id="banner">
remove
remove
</div>
ASKER
I tried:
$buffer = preg_replace('/(<div+id="b anner".*.< \/div>/',' ', $buffer);
but its not working
$buffer = preg_replace('/(<div+id="b
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))
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 and the closing div
.'</div>' // the closing div tag
.'#'; // closing delmiter
$buffer = preg_replace($banner,'', $buffer);
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);
Actually try that... so it doesn't match the first opening of any div until the last closing div... maybe...
ASKER
Can you write in in thus format please:$buffer = preg_replace('/(<div+id="b anner".*.< \/div>/',' ', $buffer)
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
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);
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 (.*)
$buffer = preg_replace('/<div([^>]+)
not really sure how to explain it what is the diffrence between (.*?) and (.*)
ASKER
oh posted it in the wrong question sorry
ASKER
HOw about:
'/\s{2,}/'