Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

Regular Expressions & preg_replace()

I see that ^ and $ are included in most expressions.  Should you include them if the search string may not be at the beginning or end of the line and you are not sure how many characters will be before or after it?

For example, I want an expression that will use preg_replace() to cycle through html docs and change all the body tags (including attributes) to a comment (<!-- body variable begins here -->).

My concern is that there will be important code before or after the body tag that I want unchanged.  It is impossible to plan on the number of characters of this code, so should I use ^ and $?  Or should I just start and end the expression with /?

My code thus far is:
[php]<?php
$text = preg_replace("/^(<body+)(.*)>+$/", "<!-- body variable begins here -->", $text)
?>[/php]

It just deletes the body tag.  The comment is not inserted.  What am I doing wrong?

I added ^ and $, but the result was the same.
0
justravis
Asked:
justravis
  • 4
  • 4
  • 2
  • +1
1 Solution
 
Big_Red_DogCommented:
Try this:

Add "is", remove +

$text = preg_replace("/^(<body)(.*)>+$/is", "<!-- body variable begins here -->", $text)
0
 
s-maximCommented:
Why a regular expression?

You can do:

$html = str_replace('<body', "<!-- body variable begins here -->\n<body", $html);

And get it all done.

This however will not work if you've got a whole load of other possibilities OR if tag body can also be in mixed case. There is stri_replace() function in CVS now, but will be only available some time later.

str_replace() is also significally faster than preg_replace.

As of preg, you could do this:

$html = preg_replace("/<body/i", "<\!-- body variable begins here -->\n<body", $html);

Hope this helps
Maxim Maletsky
maxim@php.net
0
 
justravisAuthor Commented:
Hi Maxim.  I think I hav seen your comments in the manual.

For some reason, I thought str_replace did not support regex.  Only simple replacements.

I want the code to replace the whole body tag including attributes that me be there, so I need regex.

I'll try yr sugestions and answer back today.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
justravisAuthor Commented:
Here is what happened when I tried to implement your ideas:
        #Big Red Dog @ Experts-Exchange.com
        #Result: Outputs comment, but replaces body tag as well as all text that follows.
        #$text = preg_replace("/^(<body)(.*)>+$/is", "<!-- body variable begins here -->", $text);

        #S-Maxim (maxim@php.net) @ Experts-Exchange.com
        #Result: Simply outputs comment before body tag.
        #$text = preg_replace("/<body/i", "<\!-- body variable begins here -->\n<body", $text);


Got this working pretty well:
[php]$text = preg_replace("/<(B|b)(O|o)(D|d)(Y|y)(.*)>{1}/", "<!-- body variable begins here --
>", $text);[/php]

Only problem is that it also replaces any other tags immediately following.  I'm thinking it is looking for the last '>' on that line.  Shouldn't '>{1}/' make it stop after first occurrence?  Any ideas?
0
 
justravisAuthor Commented:
Found solution, but don't understand why.

[php]$text = preg_replace("/<(B|b)(O|o)(D|d)(Y|y)(.*?)>/", "<!-- body variable begins here -->"
, $text);[/php]

Adding the question mark limited the replace to only the body tag.  It no longer replaces any tags or comments on the same line.  Does anybody know the siginifance of using '*' and '?' together?  To me, the '?' would just override the '*'.

I cut out the {1} after the '>' because it didn't seem to make a difference either way.
0
 
Big_Red_DogCommented:
Yes, "if a quantifier is followed  by  a  question  mark,
then it ceases to be greedy, and instead matches the minimum number of times possible".

This comes from http://www.php.net/manual/en/pcre.pattern.syntax.php about 1/2 way down the page.
0
 
Big_Red_DogCommented:
So is everything working for you now?
0
 
justravisAuthor Commented:
Sorry.  For the delay.  Is it possible to divy up the pioints?  If so, how?
0
 
Big_Red_DogCommented:
There are two ways:

1) Go to http://www.experts-exchange.com/Community_Support/ and ask them to split the points for http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_20521028.html however you want.

2) Award these points to one person, and then in your closing comment indicate that you have created another question with the subect of "Points for xxx" where xxx is whoever else you want to give points to, then only accept a reply from that person.
0
 
moduloCommented:
Dear justravis

I've refunded 25 points to enable you to accept the comment of Big_Red_Dog and to post a "Points for <expertname>" Q for the other expert in the same topic area.

Please:
1) Post the link to the original Q in the "Points for <expertname>" and
2) Add in the original Q a comment with the link to the "Points for <expertname>", thus the email notif will warn the expert.

modulo

Community Support Moderator
Experts Exchange
0
 
moduloCommented:
Finalized.

s-maxim points at:
http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_20570346.html

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

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.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now