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
Related
I am trying to remove the following html but my regex isn't working
<div class="vmargin"><div><iframe src="/test.php?u=N0Bhlant98C6MRj0D44HwJMuf5TdA%2F24oG9hQ2qqX6IR2IruUxVrrhLR4EpHQDvGtuHH4%2BLgJMBG6L5%2BTs6t6FfgCbo%3D&b=5&f=frame" style="width:718px;height:438px;border:0;margin:0;padding:0;" __idm_frm__="100"></iframe></div><div>Video Broken?</div></div>
I tried the following regex
preg_replace("#<div class=\"vmargin\".*?<\\/div>.*?<\\/div><\\/div>#s",'', $input);
What's wrong with it
Do not use \\ because in your closing divs, there are no \ character. Try this:
<div class=\"vmargin\".*?<\/div>.*?<\/div><\/div>
So:
$string = '<div class="vmargin"><div><iframe src="/test.php?u=N0Bhlant98C6MRj0D44HwJMuf5TdA%2F24oG9hQ2qqX6IR2IruUxVrrhLR4EpHQDvGtuHH4%2BLgJMBG6L5%2BTs6t6FfgCbo%3D&b=5&f=frame" style="width:718px;height:438px;border:0;margin:0;padding:0;" __idm_frm__="100"></iframe></div><div>Video Broken?</div></div>';
$input = preg_replace("#<div class=\"vmargin\".*?<\/div>.*?<\/div><\/div>#s", '', $string);
var_dump($input);
Output: string '' (length=0)
I know it's not technically an answer, but to make the result readable (code formatting required)
Works for me:
<?php
$input = '<div class="vmargin"><div><iframe src="/test.php?u=N0Bhlant98C6MRj0D44HwJMuf5TdA%2F24oG9hQ2qqX6IR2IruUxVrrhLR4EpHQDvGtuHH4%2BLgJMBG6L5%2BTs6t6FfgCbo%3D&b=5&f=frame" style="width:718px;height:438px;border:0;margin:0;padding:0;" __idm_frm__="100"></iframe></div><div>Video Broken?</div></div>';
echo( "!".preg_replace("#<div class=\"vmargin\".*?<\\/div>.*?<\\/div><\\/div>#s",'', $input) .'!' );
Output:
C:\test>php test.php
!!
Moving to ' quoted strings and removing the escaping makes it easier to read though
$input = '<div class="vmargin"><div><iframe src="/test.php?u=N0Bhlant98C6MRj0D44HwJMuf5TdA%2F24oG9hQ2qqX6IR2IruUxVrrhLR4EpHQDvGtuHH4%2BLgJMBG6L5%2BTs6t6FfgCbo%3D&b=5&f=frame" style="width:718px;height:438px;border:0;margin:0;padding:0;" __idm_frm__="100"></iframe></div><div>Video Broken?</div></div>';
echo( '!'.preg_replace('#<div class="vmargin".*?</div>.*?</div></div>#s','', $input) .'!' );
Same output
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)
here is what i want to do
i am working with php explode function trying to limit characters it prints after defined condition
{
$result=http://php.net
new line characters i don't want to print
$links =explode("://",$result);
$nows=$links[1];
echo $nows;
}
as you can see the above code will print
php.net
new line characters i don't want to print
but instead i want to stop printing after
php.net
You can replace newline characters with nothing:
$nows = str_replace("\n", "", $links[1]);
$nows = str_replace("\r", "", $nows);
echo $nows;
If you want only what is printed on the first line, try this:
$result = "php.net
and some other text";
$nows = reset(explode("\n", str_replace("\r\n", "\n", $result)));
If the part you're looking after will always be in the first line:
$result="http://php.net
new line characters i don't want to print";
$links = explode("\n",$result);
/*
$links[0] ->http://php.net
$links[1] ->new line characters i don't want to print
*/
$links =explode("://",$links[0]);
$nows=$links[1];
echo $nows;
/*
php.net
*/
Anyway , Consider giving more details about your case in order to offer a better way.
For instance , maybe regex?
Try
$nows = trim( $links[1] );
TRIM() will remove newlines among other things
Manual page
EDIT:
Well now we have the actual situation which you say is :-
$result=http://php.net</br>nameserver:ns1</br>nameserver:ns2.
Try
$t = explode( '</br>', $result );
$t1 = explode ( '://', $t[0] );
echo $t1[1];
Just as a note, if it is you that is creating this string somewhere else </br> is not a valid html tag, it should be <br> or if you are using XHTML it should be <br />.
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);
I have seen some solutions, or at least tries, but none of them really work.
How do I strip all tags except those inside <code> or [code] - and replace all the < and > with < etc. in order to let JavaScript do some syntax highlighting on the output?
Why don't you try using strpos() to get the position of [code] and [/code].
When you have the location (assuming you only have one set of the code tag) just get the contents of everything before and everything after and the strip_tags on that text.
Hope this helps.
Use a callback:
$code = 'code: <p>[code]<hi>sss</hi>[/code]</p> more code: <p>[code]<b>sadf</b>[/code]</p>';
function codeFormat($matches)
{
return htmlspecialchars($matches[0]);
}
echo preg_replace_callback('#\[code\](?:(?!\[/code\]).)*\[/code\]#', 'codeFormat', $code);
<?php
$str = '<b><code><b><a></a></b></code></b><code>asdsadas</code>';
$str = str_replace('[code]', '<code>', $str);
$str = str_replace('[/code]', '</code>', $str);
preg_match('/<code>(.*?)<\/code>/', $str, $matches);
$str = strip_tags($str, "<code>");
foreach($matches as $match)
{
$str = preg_replace('/<code><\/code>/', $str, '<code>'.htmlspecialchars($match).'</code>', 1);
}
echo $str;
?>
This searches for the code tags and captures what is within the tags. Strips the tags. Loops through the matches replacing the code tags with the text captured and replacing the < and >.
EDIT: the two str_replace lines added to allow [code] too.
$str = '[code]
<script type="text/javascript" charset="utf-8">
var foo = "bar";
</script>
[/code]
strip me';
echo formatForDisplay( $str );
function formatForDisplay( $output ){
$output = preg_replace_callback( '#\[code]((?:[^[]|\[(?!/?code])|(?R))+)\[/code]#', 'replaceWithValues', $output );
return strip_tags($output);
}
function replaceWithValues( $matches ){
return htmlentities( $matches[ 1 ] );
}
try this should work, i tested it and it seemed to have the desired effect.
Well, I tried a lot with all your given code, right now I am working with this one, but it is still not giving the expected results -
What I want is, a regular textarea, where one can put regular text, hit enter, having a new line, not allowing tags here - maybe <strong> or <b>....
Perfect would be to recognice links and have them surrounded with <a> tags
This text should automatically have <p> and <br /> where needed.
To fill in code in various languages one should type
[code lang=xxx] code [/code] - in the best case [code lang="xxx"] or <code lang=xxx> would work too.
Than typing the code or copy and paste it inside.
The code I am using at the moment, that at least does the changing of tags and output it allright except of tabs and linebreaks is:
public function formatForDisplay( $output ){
$output = preg_replace_callback( '#\[code lang=(php|js|css|html)]((?:[^[]|\[(?!/?code])|(?R))+)\[/code]#', array($this,'replaceWithValues'), $output );
return strip_tags($output,'<code>');
}
public function replaceWithValues( $matches ){
return '<code class="'.$matches[ 1 ].'">'.htmlentities( $matches[ 2 ] ).'</code>';
}
Similar like it works here.
The strip_tag syntax gives you an option to determine the allowable tags:
string strip_tags ( string $str [, string $allowable_tags ] ) -> from PHP manual.
This should give you a start on the right direction I hope.