How to remove < and > symbol? - php

I have a text name #chatfun <chinu,25,M,123456> i want to #chatfun chinu,25,M,123456 How to replace < and > tag.
My Code :
<?php
$text = '#chatfun <chinu,25,M,123456>';
echo strip_tags($text);
?>
Above code i am getting the blank text. How to get my actual result?

Just use str_replace with the angular brackets in an array.
echo str_replace(array('>', '<'), '', $text);
Another option is to use regex with preg_replace
echo preg_replace("/[<>]+/", '', $text);

Have you tried str_replace?
$text = str_replace(array('<', '>'), '', '#chatfun <chinu,25,m,123456>');
That will replace the unwanted characters with nothing. Only caveat is that if the characters show up anywhere else, they, too, will be replaced.

<?php
$to_remove = array("<",">");
$text = '#chatfun <chinu,25,M,123456>';
echo str_replace($to_remove,"",$text);
?>
See refrence

You're getting blank text because the text between the < and > is considered part of a tag.
Use str_replace(array('<','>'),'',$text)

Related

str_replace and preg_replace is not working

I have try to replace - by < but is not working.
$str = "JEAN-pierre, BRUNĂ”";
function replaceName($str){
//$new_str1 = preg_replace('/-/', '<', $str);
$new_str1 = str_replace("-", "<", $str);
return $new_str1;
}
that return: JEAN not JEAN<PIERRE, BRUNĂ”
Thanks you!
The code you've posted is fine. The issue you're more than likely running into is that < will be treated as the start of a HTML tag, hence the rest of the value is hidden.
You need to escape your < character before displaying it on the page.
You could use htmlspecialchars() as part of the output:
echo htmlspecialchars( replaceName( 'NAME' ) );
https://www.php.net/manual/en/function.htmlspecialchars.php

I want to echo php text but not between ()

I want to echo php text but not between (). Some thing like this =
<?php
$text = "Barry(male)";
echo $text;
?>
output =
Barry
How can i do this?
You can use preg_replace to substitute whatever is between parenthes (and the parentheses themselves) with an empty string. Like this:
<?php
$text = "Barry(male)";
echo preg_replace('#\(.*\)#', '', $text);
?>
Please note: since you didn't specify your string format, I'm assuming that the parenthesized text appears just once in the string and that there aren't nested parenthes. Otherwise, this doesn't work as expected.
Something like:
$text = "Barry(male)";
$split = explode("(", $text);
echo $split[0];
// "Barry"

Strip just <p> tags in PHP

I have a set of <p></p> tags wrapping a set of data, that data includes other tags such as <script></script> however that content could contain any number of different tags.
I just need to remove any paragraph tags from the content
Example below
$text = "<p><script>example text inside script.<script></p>";
I see the strip_tags function but I believe that will remove all the tags.
How would I go about just removing the paragraph?
Thanks.
Try,
$text = "<p><script>example text inside script.<script></p>";
$formatted_text = str_replace(['<p>', '</p>'], '', $text);
You can allow tag with strip_tags()
like this example:
$text = "<p><script>example text inside script.<script></p>";
echo strip_tags($text, '<script>');
Try this.
<?php
$text = "<p><script>example text inside script.<script></p>";
$replace = array('<p>','</p>');
echo str_replace($replace,'',$text);
http://sandbox.onlinephpfunctions.com/code/43150c7af4e7e5f572827d91abca5756213ab7ba
Version 2 (works for classes)
echo preg_replace('%<p(.*?)>|</p>%s','',$text);
http://sandbox.onlinephpfunctions.com/code/6c5414773efc1317578b5f0581b68e5acabb9a2b
Hope this helps.
You can use str_replace(),
add this line -
$text = str_replace('<p>','',$text);
$text = str_replace('</p>','',$text);
It will remove both
<p> and </p>

Remove special characters like lt; but not anchor tag

How can I remove special characters like ;lt ;gt but not Anchor tag
e.g
&lt;a href=&quot;http://www.imdb.com/name/nm0005069/&quot;&gt;Spike Jonze&lt;/a&gt; This cause by <a class="primary-black" href="http://example.com/community/RobHallums">RobHallums</a>
should be
Spike Jonze This cause by <a class="primary-black" href="http://example.com/community/RobHallums">RobHallums</a>
Here's a quick one for you:
<?php
// SET OUR DEFAULT STRING
$string = '&lt;a href=&quot;http://w...content-available-to-author-only...b.com/name/nm0005069/&quot;&gt;Spike Jonze&lt;/a&gt; This cause by <a class="primary-black" href="http://e...content-available-to-author-only...e.com/community/RobHallums">RobHallums</a>';
// USE PREG_REPLACE TO STRIP OUT THE STUFF WE DON'T WANT
$string = preg_replace('~&lt;.*?&gt;~', '', $string);
// PRINT OUT OUR NEW STRING
print $string;
All I'm doing here is looking for &lt;, followed by any character ., any number of times *, until it matches the next part of the string ?, which is &gt;.
Any time it finds that, it replaces it with nothing. So you're left with the text you want.
Here is a working demo:
http://ideone.com/uSnY0b
use html_entity_decode:
<?php $url = html_entity_decode('&lt;a href=&quot;http://www.imdb.com/name/nm0005069/&quot;&gt;Spike Jonze&lt;/a&gt;');
echo $url;
?>
the output will be:
Spike Jonze
EDIT:
<?php
preg_match_all('/<a .*?>(.*?)<\/a>/',$url,$matches);
//For Text Name
echo $matches[1][0]; //output : Spike Jonze
?>

remove all kind of tags from text

I am working on a project and I am facing a problem that "span>" also prints in the start of text I tried to remove all the tags every thing gone finely except the one i mentioned above,
here is my php code
<p>
<?php
$desc = $top_news['headline_des'];
$aa = preg_replace( '/style=(["\'])[^\1]*?\1/i', '', $desc, 2 );
if(strlen($top_news['headline_des'])>100)
{
$description = substr($aa, 1 ,850)."...";
}else{
$description = $aa;
}
echo strip_tags($description);
?>
</p>
here is the output
span >IPOR have the International Republican Institute (IRI) and the United States Agency for International....
The problem is the substr($aa, 1 ,850) call. substr starts with position 0, not 1, so what happens is this:
Input: <span>Foobar</span>
substr($input, 1, 850)
Output: span>Foobar</span>
substr cuts off happily the first char. Hence, strip_tags doesn't recognize span> as a whole tag and simply leaves it alone.
Fix: Use substr($aa, 0, 850).
$intro = ereg_replace("[</*>]", "", $intro);
$text = preg_replace("/<.+?>/", "", $text);
something like this should remove any tag in the $text variable.
If it doesn't work, you should check if the initial text you want to remove the tags from is correctly formed. For example a span > won't be removed since it isn't a tag, but a <span > would be removed without problem.
you could use strip_tags only for removing tags
With preg_replace
$desc = $top_news['headline_des'];
$search = array(
'#<style[^>]*?>.*?</style>#siU',
'#<[\/\!]*?[^<>]*?>#si'
);
echo $pregReplacedContent = preg_replace($search, "", $aa);

Categories