mOrloff 11-09-2011, 11:37 PM This seems like it would be a pretty simple REGEX, but even simple REGEX seems to trip me up :D
I have special opening and closing blocks in my file ("<!--O-->" and <!--C-->", respectively), and I'm trying to select everything in that block (working toward the goal of replacing it).
Here's my latest attempt
$subject = 'fsdjf idsfi sidh dsfh <!--O--> Hi there. <!--C--> sdofijos.dsflsad';
$pattern = '/\<!--O--\>*.\<!--C--\>/';
preg_match($pattern, $subject, $matches);
print_r($matches);
I'm trying to find "<!--O--> Hi there. <!--C-->", but $matches continues coming up empty.
What am I missing?
~ Mo
MattF 11-09-2011, 11:41 PM Try:
$pattern = '~\<\!--O--\>*.\<\!--C--\>~';
mOrloff 11-09-2011, 11:48 PM Hrmph!
Still no luck.
My test block now consists of:
$subject = 'fsdjf idsfi sidh dsfh <!--O--> Hi there. <!--C--> sdofijos.dsflsad';
$pattern = '~\<\!--O--\>*.\<\!--C--\>~';
preg_match($pattern, $subject, $matches);
print_r($matches);
Other thoughts??
~ Mo
MattF 11-09-2011, 11:52 PM You have your syntax slightly incorrect. Just noticed it. *. should be .*
mOrloff 11-09-2011, 11:57 PM Ahah!
That works.
Thanks.
Spookster 11-10-2011, 12:00 AM Dang it. I typed in the answer and then went to the bathroom and came back and you beat me to the punch. :mad:
$pattern = '/\<!--O--\>.*\<!--C--\>/';
* says 0 or more and the . indicates any character so that means match any pattern with the beginning and end you specified that has 0 or more characters in between them.
mOrloff 11-10-2011, 12:04 AM Furthermore, I would probably end up saving myself some effort if I were only selecting what was between those tags (so only returned "Hi there.").
Is that easy to do, or would that become a disaster??
What key words should I be searching?
~ Mo
Fou-Lu 11-10-2011, 12:10 AM Wrap .* in ( and ). That will return it in its own subpattern match.
mOrloff 11-10-2011, 12:25 AM Cool, that worked.
If my end goals to use it directly in a preg_replace() call, though, how would I do that?
Or would I have to run a preg_match(), and then just work with $matches[1]?
~ Mo
Spookster 11-10-2011, 12:38 AM Cool, that worked.
If my end goals to use it directly in a preg_replace() call, though, how would I do that?
Or would I have to run a preg_match(), and then just work with $matches[1]?
~ Mo
<?php
$replacement = "All Hail Spookster";
$subject = 'fsdjf idsfi sidh dsfh <!--O--> Hi there. <!--C--> sdofijos.dsflsad';
$pattern = '/<!--O-->(.*)<!--C-->/';
echo htmlspecialchars($subject) . "</br>";
$subject = preg_replace($pattern, $replacement, $subject);
echo htmlspecialchars($subject) . "</br>";
?>
Output:
fsdjf idsfi sidh dsfh <!--O--> Hi there. <!--C--> sdofijos.dsflsad
fsdjf idsfi sidh dsfh All Hail Spookster sdofijos.dsflsad
Fou-Lu 11-10-2011, 12:46 AM heh heh
:D
MattF 11-10-2011, 12:46 AM Dang it. I typed in the answer and then went to the bathroom and came back and you beat me to the punch. :mad:
Should have held it. :D :D
mOrloff 11-10-2011, 12:47 AM Thanks.
And if I only wanted to select the "Hi there" so that my special little blocks remain after the preg_replace(), how would I need to modify my regex?
~ Mo
MattF 11-10-2011, 12:57 AM $replacement = "\\1All Hail Spookster\\2";
$subject = 'fsdjf idsfi sidh dsfh <!--O--> Hi there. <!--C--> sdofijos.dsflsad';
$pattern = '~(<!--O-->).*(<!--C-->)~';
or:
$replacement = "\\1All Hail Spookster\\3";
$subject = 'fsdjf idsfi sidh dsfh <!--O--> Hi there. <!--C--> sdofijos.dsflsad';
$pattern = '~(<!--O-->)(.*)(<!--C-->)~';
Spookster 11-10-2011, 01:02 AM <?php
$replacement = "All Hail Spookster";
$subject = 'fsdjf idsfi sidh dsfh <!--O--> Hi there. <!--C--> sdofijos.dsflsad';
$pattern = '/(<!--O-->)(.*)(<!--C-->)/';
echo htmlspecialchars($subject) . "</br>";
$subject = preg_replace($pattern, "$1 $replacement $3", $subject);
echo htmlspecialchars($subject) . "</br>";
?>
Output:
fsdjf idsfi sidh dsfh <!--O--> Hi there. <!--C--> sdofijos.dsflsad
fsdjf idsfi sidh dsfh <!--O--> All Hail Spookster <!--C--> sdofijos.dsflsad
mOrloff 11-10-2011, 01:07 AM Ohhh, I see it :)
Thanks you guys!
~ Mo
mOrloff 11-11-2011, 12:10 AM My regex seems to be tripping over new lines.
The pattern is currently looking for .* between the special blocks.
I'd like it to find/replace a match even if there are new lines in there.
Is there an ANY character symbol??
~ Mo
MattF 11-11-2011, 12:14 AM $pattern = '~(<!--O-->)(.*)(<!--C-->)~m';
Spookster 11-11-2011, 12:15 AM My regex seems to be tripping over new lines.
The pattern is currently looking for .* between the special blocks.
I'd like it to find/replace a match even if there are new lines in there.
Is there an ANY character symbol??
~ Mo
By new lines do you mean like this?
fsdjf idsfi sidh dsfh <!--O--> [CRLF]
Hi there. <!--C--> sdofijos.dsflsad [CRLF]
MattF 11-11-2011, 12:17 AM Would m catch that or would that need the s modifier? (I haven't arsed around with regex for a good while, so I'm a lot rusty).
mOrloff 11-11-2011, 12:29 AM By new lines do you mean like this?
fsdjf idsfi sidh dsfh <!--O--> [CRLF]
Hi there. <!--C--> sdofijos.dsflsad [CRLF]
To be honest, the details behind the new lines curtain elude me.
All I know is when I het Enter, it makes a new line :)
The content we are trying to replace is basic html typed out by hand, but when I'm echoing from php, I use "\n".
I hope that answered our question.
And what about MattF's modifiers suggestion??
Where in the pattern do i put modifiers? (I tried '~<!--O-->.*<!--C-->/m~', but no dice)
FYI: I did try switching the delimiter from / to ~ to avoid any potential problems when trying it with /m.
~ Mo
Spookster 11-11-2011, 12:29 AM Would m catch that or would that need the s modifier? (I haven't arsed around with regex for a good while, so I'm a lot rusty).
Yes that should work. The s modifier should also work in this case. Should point out that in all my examples I am using the / as the delimiter and you are using the ~. Might confuse people when they look at both examples and wonder why they are different.
Spookster 11-11-2011, 12:33 AM To be honest, the details behind the new lines curtain elude me.
All I know is when I het Enter, it makes a new line :)
The content we are trying to replace is basic html typed out by hand, but when I'm echoing from php, I use "\n".
I hope that answered our question.
And what about MattF's modifiers suggestion??
Where in the pattern do i put modifiers? (I tried '~<!--O-->.*<!--C-->/m~', but no dice)
~ Mo
You are mixing delimters. That should either be
$pattern = '~(<!--O-->)(.*)(<!--C-->)~m';
or this
$pattern = '/(<!--O-->)(.*)(<!--C-->)/m';
mOrloff 11-11-2011, 12:47 AM Oh, I see.
I thought ... well never mind.
I'm still missing the target.
To test, I went back to our test code and juiced up the $subject. When I try this, there is no match.
$pattern = '/<!--O-->.*<!--C-->/m'; // for testing
$subject = 'fsdjf idsfi sidh dsfh <!--O-->'."\n".' Hi there. '."\n".'<!--C--> sdofijos.dsflsad'; // for testing
$subject = preg_replace($pattern, $replacement, $subject); // for testing
MattF 11-11-2011, 12:55 AM Try it with s instead of m, or sm.
Yes that should work. The s modifier should also work in this case.
Cheers. I couldn't for the life of me recall if there were any caveats with the m modifier. :D
Should point out that in all my examples I am using the / as the delimiter and you are using the ~. Might confuse people when they look at both examples and wonder why they are different.
I'll start using the more common / in examples, save creating confusion.
mOrloff 11-11-2011, 01:13 AM Awesome!
I tried sm, BINGO!
So I also tried just s, and we're still golden.
Again, thanks-a-bunch.
~ Mo
|
|