Jump to content

Recommended Posts

New to this... can I use str_replace to replace all instances of a specific word or html tag on a page?

 

I have an html page containing elements produced elsewhere and which I can't remove by simply editing the html.

 

If I've understood str_replace properly, I can add a bit of php to the page and it'll modify things - so in this way I'm hoping to use it to remove some unwanted br tags from code which I can't directly edit.

Edited by Heyes
Link to comment
https://forums.phpfreaks.com/topic/301922-understanding-and-using-str_replace/
Share on other sites

Need some more context. When you say "have an Html page", what does that mean if you can't directly edit it? The page is on the screen? And you want to alter it? Very confusing. And how are you going to "add a bit of php to it" if you can't directly edit it?

Need some more context. When you say "have an Html page", what does that mean if you can't directly edit it? The page is on the screen? And you want to alter it? Very confusing. And how are you going to "add a bit of php to it" if you can't directly edit it?

 

Thanks.

 

Part of the page is generated by a third-party app - similar to how, for example, Disqus comment system works. (In this particular case, the code comes from an on-site membership script - and to modify I'd have to edit the core... which I don't want to do.

 

So, although I can edit the page, I can't modify all parts... hence the hope for using str_replace to modify things.

 

I now realise that my initial understanding of it was wrong... I thought it would work in a 'global search and replace' way, through a single bit of php positioned somewhere in the code. I now know that I have to position it directly where the replacement is to happen.

 

So - problem sort of solved.

I have no idea what you have done. A php function operates in a script on a string that is part of that script, whether it is a variable or some data that is read into that script. How you are doing this on a 'page' is beyond me. But if you are happy...

I have no idea what you have done. A php function operates in a script on a string that is part of that script, whether it is a variable or some data that is read into that script. How you are doing this on a 'page' is beyond me. But if you are happy...

 

As an example, my page html contains <?php echo $frm['elements']['product_id_page-0-0']['htmle'] ?> from a third-party membership app. The code adds br tags which I don't want.

 

By using <?php echo str_replace('<br />', '', $frm['elements']['product_id_page-0-0']['htmle']) ?>, those br tags are removed without the need to modify the core code.

 

There are several instances of similar code in a page, and I add the str_replace to each.

 

When I originally posted my question, I'd mistakenly thought that I could add one str_replace to the whole page and have all br tags removed. I now realise that str_replace has to be added to each element to be modified.

Edited by Heyes
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.