Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP substring delete

Tags:

string

php

How do I remove entire characters from a string starting from a particular string position to another position, and shift the rest to merge with this starting position?

I'm working with HTML contents. So take this for example.

</a></h4><p class="pdesc">When a private satellite encounters .... and it is found to ...</p></div></div>.....<p class="pdesc">Before leavingon his .... </p> ...... <p class="pdesc"> .... </p>

So now I want to remove all characters in between

<p class="pdesc"> - to - </p>.

of every occurrence.

I can search for this (strpos) and get the starting and the ending positions, but then manually deleting them and shifting strings from then on will be too tedious.

Is there a built in function or a simple way to do it?

like image 446
Quik Tester Avatar asked Jan 22 '26 23:01

Quik Tester


1 Answers

You can do this with the substr_replace­Docs function:

$var = 'ABCDEFGH:/MNRPQR/';

/* Delete 'MNRPQR' from $var. */
echo substr_replace($var, '', $start = 10, $length = -1) . "<br />\n";
like image 149
hakre Avatar answered Jan 25 '26 12:01

hakre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!