• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

Can anyone explain the following regex?

$str = preg_replace('/[\n\r\t]+/', '', $str);
0
Nura111
Asked:
Nura111
  • 13
  • 11
1 Solution
 
DerokorianCommented:
It finds and removes all line breaks, carriage returns, and tab characters from the string.
0
 
Nura111Author Commented:
Thank you!
HOw about:

'/\s{2,}/'

0
 
DerokorianCommented:
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
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.

 
DerokorianCommented:
'/\s{2,}/' Finds spaces in groups of 2 or more (ie: whenver there are two  spaces  between  a  word  instead of one like normal)
0
 
Nura111Author Commented:
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?

0
 
DerokorianCommented:
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
0
 
Nura111Author Commented:
geex you know regex!
Thank you!
0
 
Nura111Author Commented:
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

0
 
DerokorianCommented:
What's the error?
0
 
Nura111Author Commented:
Parse error: syntax error, unexpected '='
0
 
DerokorianCommented:
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 (
0
 
Nura111Author Commented:
oh god whats wrong with ???sorry I dont know how i ddit notice that
0
 
DerokorianCommented:
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.
0
 
Nura111Author Commented:
What is the meaning of this sign alone ^ in a regex?
0
 
DerokorianCommented:
^ means not including so...
[^>] means all characters not including greater than.
0
 
Nura111Author Commented:
Im tryng to create a regex that will remove all the character between a certain div
for exmp:

<div id="banner">
remove
remove

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

but its not working
0
 
DerokorianCommented:
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
0
 
DerokorianCommented:
$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...
0
 
Nura111Author Commented:
Can you write in in thus format please:$buffer = preg_replace('/(<div+id="banner".*.<\/div>/','', $buffer)
0
 
Nura111Author Commented:
I tried the first one its not working I have to add somehing for the spaces though,
here is the new question,
http://www.experts-exchange.com/Software/VMWare/Q_27412639.html
0
 
DerokorianCommented:

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

Open in new window

0
 
Nura111Author Commented:
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 (.*)
0
 
Nura111Author Commented:
oh posted it in the wrong question sorry
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 13
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now