Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7927473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:19:20+00:00 2026-06-03T19:19:20+00:00

Basically, what I’m trying to do is search through a rather large PHP file,

  • 0

Basically, what I’m trying to do is search through a rather large PHP file, and replace any block of PHP code that includes the string “search_term” somewhere in it with some other code. I.e.

<?php
//some stuff
?>
<?php
// some more stuff
$str = "search_term";
// yes...
?>
<?php 
// last stuff
?>

should become

<?php
//some stuff
?>
HELLO
<?php 
// last stuff
?>

What I’ve got so far is

$string =~ s/<\?php(.*?)search_term(.*?)\?>/HELLO/ims;

This correctly matches the closest closing ?>, but begins the match at the very first <?php, instead of the one closest to the string search_term.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:19:21+00:00Added an answer on June 3, 2026 at 7:19 pm

    Generally, I don’t like to use non-greedy matching, because it usually leads to problems like this. Perl looks at your file, finds the first '<?php', then starts looking for the rest of the regexp. It passes over the first '?>' and the second '<?php' because they match .*, then finds search_term and the next '?>', and it’s done.

    Non-greedy matching means that you have a regular expression that matches more things than you really want, and it leaves it up to perl to decide which match to return. It’s better to use a regular expression that matches exactly what you want to match. In this case, you can get what you want by using ((?!\?>).)* instead of .*? ((?!\?>) is a negative look-ahead assertion)

    s/<\?php((?!\?>).)*search_term((?!\?>).)*\?>/HELLO/is;
    

    If you expect multiple matches, you might want to use /isg rather than /is.

    Alternatively, just split the file into blocks:

    @blocks = split /(\?>)/, $string;
    while (@blocks) {
        $block = shift @blocks;
        $sep = shift @blocks;
        if ($block=~/search_term/) {
            print "HELLO";
        } else {
            print $block, $sep;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I'm trying to make a web coding tutorial site (awesome that i cant
Basically I am trying to restart a service from a php web page. Here
Basically I'm trying to accomplish the same thing that mailto:bgates@microsoft.com does in Internet Explorer
Basically, I'm trying to compile CEF on VS2008 EE. The IDE complains that it
Basically I am trying to search the page when it loads for the string
Basically I have three pages. -A page with all possible search results for example:
Basically, I have the following code: public class MyDictionary<TKey, TValue> : IDictionary<TKey, TValue> {
Basically, I don't understand why the code below will output 434 when 4.35 *
Basically, what I'm trying to create is a page of div tags, each has
Basically from a database I am getting data that is formatted like this nameofproject101

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.