How to replace all <br /> using str_replace? - php

New to this, I'm trying (and failing) to use str_replace to replace all tags in an html page.
In trying to learn what I'm doing, I can use it with echo to output a modified phrase... but not to replace anything.
I'm using: I'm using <?php // $el = str_replace('<br />', '', $el); ?>
My very limited understanding is that...
< ?php
$str_rep = "This is the original phrase";
echo str_replace("original","modified",$str_rep);
?>
... will modify and display a phrase wherever the code is positioned, but I don't understand how to modify/use to change something already in the code.

It's rather easy:
echo str_replace("<br/>","something else",$text);
Please google before asking on SO.

Your code seems good to me. I used this:
<?php
$el = 'Some text with tags.<br />and no other things<br />Nice!';
$el = str_replace('<br />', '', $el);
echo $el;
and it outputs:
Some text with tags.and no other thingsNice!
Maybe your input is not correct or you are trying to replace the wrong (written) tag?

Related

How to echo php code and use it?

<?php echo $row["html"]; ?>
Inside of the $row["html"] there's:
<?php $Site->Nav($owner); ?>
but when I echo it, it only echoes:
Nav($owner); ?>
How may I print the full and make it usable, which means that it will print the function Nav?
I've tried to replace <?php with [[// i the database, and just before echoing it, I change back with replace. But without success
I think you need to use eval function of php. See the example below.
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
Might be it can help.
Use eval function. It might solve your problem like this:
<?php echo eval($row["html"]); ?>
Keep the code as is in DB as if you are writing it in PHP file but without PHP opening and closing tags i.e. <?php and ?>. I haven't checked this (as i am not sure what $Site->Nav($owner); will do) but hope it would work in this case.
If I understand correctly you are wanting to output the results of $Site->Nav($owner);
I have no idea what this is expected to output, but assuming it is a string of some kind that you wish to display (hence echo) - an example of achieving this would be calling your code and have that method return the value, so you can echo it out. Ie:
function Nav($owner){
// Do your stuff
return 'Your Desired Output';
}
Then on your page you would have
<?php echo $Site->Nav($owner); ?>
Which would echo "Your Desired Output".

Using strip_tags with custom exclusion

In my code I need so strip all tags from a string excluding tag with inline "display:none" attribute, so tags like this:
<div style="display:none"></div>
should stay intact.
What would be the best way to achieve this? Is there any way I can make strip_tags work this way?
You can pass second parameters (want to exclude) to strip_tags as like
$str = "<html><div style='display:none'></div></html>";
echo strip_tags($str, '<div></div>');
Output will be <div style='display:none'></div>
You can use regex /<[^>]*>/
<?PHP
$val = preg_replace('/<[^>]*>/', '', '<div style="display:none">tessss</div>') ;
echo $val ;
?>
strip_tags only work on visible tags but regex is the best way
I finally did it like:
$div = preg_replace('/<(?!/?div style="display:none)[^>]+>/','',$div);

replacing new line with br error

I'm using a comment box to submit comments. Users when entered any comment and press enter for a new line then after submit I used to update it to database using $update=mysql_real_escape_string($update); for security purpose.
But when that comment is shown the new line was replaced by alphabet 'n' and the whole sentence which should come like this
John is awesome
He loves food
comes like:
John is awesome nHe loves food
I searched the error in stack and found a solution:
I replaced <?php echo $message; ?> with <?php echo str_replace('\n',"<br/>", $message); ?> which did the job. but when I tried commenting 'n' it comes out to be \'n\' any solution for that?
nl2br will do the trick. It's a built-in function that will add newlines using the <br> tag.
echo nl2br($message);
Updated:
$output = htmlspecialchars($message);
$output = str_replace('\\n','<br/>', $output);
$output = stripslashes($output);
echo $output;
stripslashes should take care of the \'n\' problem as well as many others.
Note that the order of these commands matters. If you strip slashes first, you’ll mess up the str_replace. If you use htmlspecialchars after str_replace, it will mess up your <br/>.
Why don't you use the native nl2br() function ?
$foo = nl2br("I love\n yo");
yields I love<br /> you.
try to use <?php $var = preg_replace("[\n]","<br/>",$comment); ?>
Example :
<?php
$text = "John is awesome
He loves food";
$echo = preg_replace("[\n]","<br/>",$text);
echo $echo;
?>

How to find content using PHP regular expression

I want to find content inside of a DIV. I just want to specify the DIV’s id; the rest is automatically adjusted under preg_match.
E.g.:
<div id="midbuttonarea" etc...>some text...</div>
Brenton is right. Anyway, here you have:
<?php
function findDivInnerHtml($html, $id){
preg_match("/<div .* id=\"{$id}\" .*>(.*)<\\/div>/i", $html, $matches);
return $matches[1];
}
?>
Sample usage:
<?php
$html = '<div id="other"> xxx </div> <div id="midbuttonarea" etc...>some text...</div> ';
$innerHTML = findDivInnerHtml($html, 'midbuttonarea');
echo $innerHTML; //outputs "some text..."
?>
Hope this helps.
You're much more likely to get a good result using an HTML parser to parse HTML instead of Regex. It's extremely difficult to parse HTML with Regex, and the result may not be very reliable.
Check the accepted answer to this question: How do you parse and process HTML/XML in PHP? for some suggestions on how to go about it.

Changing Text in PHP

I haven't found anytihng in Google or the PHP manual, believe it or not. I would've thought there would be a string operation for something like this, maybe there is and I'm just uber blind today...
I have a php page, and when the button gets clicked, I would like to change a string of text on that page with something else.
So I was wondering if I could set the id="" attrib of the <p> to id="something" and then in my php code do something like this:
<?php
$something = "this will replace existing text in the something paragraph...";
?>
Can somebody please point me in the right direction? As the above did not work.
Thank you :)
UPDATE
I was able to get it working using the following sample:
Place this code above the <html> tag:
<?php
$existing = "default message here";
$something = "message displayed if form filled out.";
$ne = $_REQUEST["name"];
if ($ne == null) {
$output = $existing;
} else {
$output = $something;
}
?>
And place the following where ever your message is to be displayed:
<?php echo $output ?>
As far as I can get from your very fuzzy question, usually you don't need string manipulation if you have source data - you just substitute one data with another, this way:
<?php
$existing = "existing text";
$something = "this will replace existing text in the something paragraph...";
if (empty($_GET['button'])) {
$output = $existing;
} else {
$output = $something;
}
?>
<html>
<and stuff>
<p><?php echo $output ?></p>
</html>
but why not to ask a question bringing a real example of what you need? instead of foggy explanations in terms you aren't good with?
If you want to change the content of the paragraph without reloading the page you will need to use JavaScript. Give the paragraph an id.<p id='something'>Some text here</p> and then use innerHTML to replace it's contents. document.getElementById('something').innerHTML='Some new text'.
If you are reloading the page then you can use PHP. One way would be to put a marker in the HTML and then use str_replace() to insert the new text. eg <p><!-- marker --></p> in the HTML and $html_string = str_replace('<!-- marker -->', 'New Text', $html_string) assuming $html_string contains the HTML to output.
If you are looking for string manipulation and conversion you can simply use the str_replace function in php.
Please check this: str_replace()
If you're using a form (which I'm assuming you do) just check if the variable is set (check the $_POST array) and use a conditional statement. If the condition is false then display the default text, otherwise display something else.

Categories