Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php delete content-type frm text

Tags:

regex

php

I have some text like $text="--e89a8f234aade3345704b8477b83 Content-Type: text/plain; charset=ISO-8859-1 this is a text. this is a text. --e89a8f234aade3345704b8477b83 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable this is a text.";

I want the output as $output="this is a text. this is a text.";

One more eg:- $text="--14dae9340ba954ae0704b84acde9 Content-Type: text/plain; charset=ISO-8859-1 First Name: aaa Last Name: aaa --14dae9340ba954ae0704b84acde9 Content-Type: text/html; charset=ISO-8859-1 First Name: James Last Name: Cummings --14dae9340b";

Output should be $output="First Name: aaa Last Name: aaa";

I am not getting how to do this.

like image 758
user930026 Avatar asked Mar 06 '26 07:03

user930026


2 Answers

In case you are sure that charset is ISO-8859-1 and string does not contain -- :

$a=explode("charset=ISO-8859-1",$text);
$b=explode("--",$a[1]);
$output=$b[0];

If you're still sure about the charset, but not the --:

$aa=substr($text,0,30);
$a=explode("charset=ISO-8859-1",$text);
$b=explode($aa,$a[1]);
$output=$b[0];

And if you're not even sure about the charset:

$aa=substr($text,0,30);
$a=explode("charset=",$text);
$a2=explode(" ",$a[1]);
$b=explode($aa,$a2[1]);
$output=$b[0];
like image 66
axiomer Avatar answered Mar 07 '26 22:03

axiomer


Use preg_match over the string, something like

<?php
$text="--14dae9340ba954ae0704b84acde9 Content-Type: text/plain; charset=ISO-8859-1 First     Name: aaa Last\
 Name: aaa --14dae9340ba954ae0704b84acde9 Content-Type: text/html; charset=ISO-8859-1 First Name: James\
 Last Name: Cummings --14dae9340b";

$cnt = preg_match_all('/ISO-8859-1 (.+?) --/',$text,$array);
$array[1][0], "<br />",$array[1][1];

?>

The second element is $array (position 1) contains all the substrings of $text that match the group in the pattern.

like image 40
msgmash.com Avatar answered Mar 07 '26 22:03

msgmash.com



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!