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

  • Home
  • SEARCH
  • 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 9167575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:25:28+00:00 2026-06-17T15:25:28+00:00

Possible Duplicate: PHP, preg_replace, replace tag with tag attr I read many references but

  • 0

Possible Duplicate:
PHP, preg_replace, replace tag with tag attr

I read many references but this is very hard to understand.

Please help to improve my code.

I want to replace

<script src="core.js"></script> to <temp src="core.js"></temp>

While I am testing it, below will actual output what I want. However, on $replace, I only put <temp, how come it could replace the end tag???

$string = '<script src="core.js"></script>';
$pattern = '/^<script|<\/script>$/i';
  //    ^(start from script)    | (or)    $(end with script)
$replace = '<temp';
echo preg_replace($pattern, $replace, $string);

Out Put

<temp src="core.js"></temp>

Thank you very much for your help and advice.

  • 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-17T15:25:29+00:00Added an answer on June 17, 2026 at 3:25 pm

    Sorry if this doesn’t answer your question because you asked about regex, but if you can use str_replace, its generally better to do so, and much quicker.

    $result = str_replace('<script', '<temp', $string);
    $result = str_replace('script>', 'temp>', $result);
    

    The problem with this though is, it wont match strings like:

    <script src="core.js"></script >
    

    The reason i didn’t just do:

    str_replace("script", "temp", $string);
    

    is because you could have other mensions of the word “script” in your variable, for example if a js file was called example.script.js

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: php preg_replace \ I am trying to replace all occurances of the
Possible Duplicate: Find url from string with php I found this code: but 2
Possible Duplicate: php regular expression help? hi, i want to replace i like apple
Possible Duplicate: Regular expression to remove anything but alphanumeric & spaces (in PHP) I'm
Possible Duplicate: JSON Parsing with PHP I'm hoping for a little assistance on this.
Possible Duplicate: preg_replace how surround html attributes for a string with " in PHP
Possible Duplicate: How to parse and process HTML with PHP? I expect this one
Possible Duplicate: PHP: self vs. $this What does $this-> mean in CakePHP? Please answer
Possible Duplicate: php == vs === operator I understand the difference between === ,
Possible Duplicate: PHP regex for Lebanese phone number preg_replace to mask parts of a

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.