3

Good day,

I want to remove everything before and after a certain character sequence in a string.

E.g:

$string = 'TESTING<html>This is a string</html>ANOTHER TEST!!';

I want everything before <html> and after </html> to be removed, so that the string looks like this when I write it to a database:

<html>This is a string</html>

Important thing here is that <html> and </html> keep existing in the string.

Anyone got an idea?

2
  • '<html>' is not a character, it's a string
    – BDL
    Jul 24, 2015 at 9:15
  • 2
    OP is talking about a "character sequence".
    – t.h3ads
    Jul 24, 2015 at 9:18

3 Answers 3

5

Can be done with a regular expression:

$str = 'TESTING<html>This is a string</html>ANOTHER TEST!!';
preg_match_all('/(<html>.*<\/html>)/', $str, $matches);
print_r($matches[1][0]);

For a multiline string, you have to add the modifier "m":

$str = 'TESTING<html>This is a string</html>ANOTHER TEST!!';
preg_match_all('/(<html>.*<\/html>)/m', $str, $matches);
print_r($matches[1][0]);
8
  • Don't know why but this is returning an empty array. Array ( [0] => Array ( ) [1] => Array ( ) ) Jul 24, 2015 at 9:25
  • Is your regular expression correkt? Here is a working example: 3v4l.org/rUmBu
    – t.h3ads
    Jul 24, 2015 at 9:27
  • Do you have line breaks in your string? Then you have to add the modifier "m" to the regular expression. I will update my answer.
    – t.h3ads
    Jul 24, 2015 at 9:38
  • Yeah it's a fetched imap email, the update still returns an empty array. Jul 24, 2015 at 9:47
  • That will be quite hard since the string that needs to be modified is an HTML email message fetched with the imap function. The string contains the HTML message but before the HTML, it also contains the receiving mailserver information which I'm trying to strip out. Jul 24, 2015 at 9:52
3

Try something like this:

// vars
$string = ' This is text before <html> and this is the text in between </html> And after there is some more text.. ';
$first = '<html>';
$last = '</html>';
// job
$start = stripos($string, $first); // first occurrence position 
$length = strripos($string, $last); // last occurrence position 
$newString = substr($string, $start, ($length-$start+strlen($last))); 
// output
var_dump($string,$start,$length,$newString);
2
  • Curious ... Here is a working example 3v4l.org/cVp49 (I don't know 3v4l.org before thx stackoverflow.com/users/3446241/typoheads)
    – Nolwennig
    Jul 24, 2015 at 12:26
  • @ Nolwennig Try this and you'll see what I mean: $string = ' This is text before <html> and this is the text in between </html> And after there is some more text.. '; $start = stripos($string, '<html>'); // first occurrence position $length = strripos($string, '</html>'); // last occurrence position $newString = substr($string, $start, $length); var_dump($string,$start,$length,$newString); Jul 27, 2015 at 13:08
0

Along with the posted answers, you can also use strstr() function to get the text before and after like;

$newstring = '<html>' . strstr($string, '<html>');
$newstring = strstr($newstring, '</html>', true) . '</html>';
1
  • Notice: Undefined variable sting
    – Nolwennig
    Jul 17, 2017 at 12:41

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.