str_replace not working in html codes - php

i'm working with a form one of its fields is an RTF textarea (jWYSIWYG), that is autofilled with some database information at the load of the page, all that using Symfony framework. This RTF editor can add some html tags like <p>,<b>, etc.
The trouble starts when i try to know if the textarea has been modified before sending the form: what i get from the $request is that all the html tags are coded like <p>,<b>, etc. I tryed to replace that expressions with the < and > characters so i can compare it to the stored data.
$codes = array('>','<');
$chars = array('<' ,'>' );
return str_replace($codes,$chars,$text);
but this function returns me the same array i pass as parameter of the str_replace function. What am I doing wrong? have anyone had the same problem?

Try this function instead of str_replace: http://www.php.net/manual/en/function.htmlspecialchars-decode.php

Finally discovered the problem. Was not about the html tags! the problem is (i dont know why) the jWYSIWYG adds about 24 white spaces at the end of the field, so obvously, the comparation between stored and new data results different.
i simply deleted the final whitespaces of the input this way:
$text = rtrim($text);

Related

My php function does what I want but ouput converts characters to ascii

I am using the following function (tried both in my Wordpress Child Theme function file as well as a plugin - works in both cases) to remove hashtags from post titles. The function does what I want but then all titles (post body content is ok just titles) are now showing HTML numbers for characters (i.e., ' = &8217; and & = &038; and - = &8211;).
So this title
That's #testing the & and apostrophe #tagstitle #cats #cat #instagramcats
becomes
That&8217;s testing the &038; and apostrophe
Which removes hashtags as desired but creates the character issue.
function remove_hashtags($string){
return preg_replace('/#(?=[\w-]+)/', '',
preg_replace('/(?:#[\w-]+\s*)+$/', '', $string));
}
add_filter('the_title', 'remove_hashtags');
I've tried adding additional code:
html_entity_decode('the_title', ENT_QUOTES | ENT_XML1, 'UTF-8');
to the function after reading up on PHP (I'm just learning) but it doesn't seem to work and I'm not sure how to use
html_entities($string)
(question update adding more information)
I basically took the code from here - that had exactly what I needed. I just added the last lines for filtering the WordPress Post Titles.
I don't want to make it too complicated a question but ideally I would like to remove the hashtags from the text and actually create post tags from them. I have found several answers for each part I just don't know how to put it all together. Forget that though...I really just want to find out why all of the sudden the ascii numbers are replacing the original punctuation.
If you only want to remove hashtags you could use str_replace("#","",$string);

Problems with Textareas, New Lines and nl2br

I'm having trouble getting data out of the database and echoing out in a HTML page textarea.
This is the code used to get the data into the database:
$_SESSION['content'] = mysqli_real_escape_string($link, strip_tags($_POST['content'],'<a>'));
This simply strips HTML tags except for links and stores in the database. If you look in the database, the line breaks are invisible but there, so i assume they are \n and \r.
If i were to type into a textarea:
This should be a
New line
The database stores this as:
This should be a<br>
New line
When echoed out into a textarea, this is what's displayed:
This should be a \r\n\r\nNew line
I'm sure i'm missing something very simple, any help greatly appreciated.
UPDATE:
If i remove mysqli_real_escape_string, the line breaks are preserved and work perfectly, do I have to sacrifice security for this?
SOLVED:
mysqli_real_escape_string causing the problem, do not echo out a variable which has had this applied. Only use mysqli_real_escape_string when inserting, deleting, etc from a database, not before, definitely not after ;)
Thanks everyone!
Use the correct HTML/CSS.
;-)
The line breaks all work in an HTML pre tag, or in a tag with the CSS white-space property set to:
white-space: pre;
Resources:
http://www.w3schools.com/cssref/pr_text_white-space.asp
http://www.quirksmode.org/css/whitespace.html
Firstly, you shouldn't be using nl2br as this will change your \n's to <br>'s.
You will most likely need to strip the slashes echo stripslashes($_SESSION['content']).
Edit following further comments:
If the data is stored as <br>'s in the database, you can just do str_replace('<br>',"\n",$string); which will convert the <br>'s into \n's
This worked for me:
function br2nl( $input ) {
return preg_replace( '/\<br.*\>/Ui', '', $input );
}
For very long lines, you also need the text to wrap instead of potentially break out of its parent container. To cover this case, but also preserve new lines, use following css:
white-space: pre-wrap;
resource: https://css-tricks.com/almanac/properties/w/whitespace/
The nl2br function is working with that content as a string:
http://codepad.org/M2Jw9KQJ
This leads me to believe that you are not echoing out the content you claim you are.
Are you sure the content in the DB is as you say it is?
Perhaps you need to go the other way round:
function br2nl( $data ) {return preg_replace( "!<br.*>!iU", "\n", $data );}

Change url encode entities into html characters?

I used the jquery function serialize() for a form, made an ajax call, and use php to do the form processing.
I have a textarea in that form where users type uses spaces and line breaks. I can access the values with $_POST, but its doesnt interpret the line breaks into html <br/ > tags. Is there a function that converts line breaks the urlencoded string into <br/> tags and other html tags? Or is everything already decoded by the time i access it with $_POST that i cant do anything with it?
Use nl2br($text); (new-line to break-rule)
Are you sure you are using nl2br() while echoing textarea's value ?
edit: too late:P
in PHP you have a function to add <br> before any new line : nl2br()
You should use it only when you print the text in the textarea with echo, not for storing in your Database.
You also can find a use of addslashes() to avoid any problems with the caracteres ' and " in your textarea
Alternatively, if you don't want to apply html tags into the value, you can later display the line spacing correctly by applying the CSS white-space property into the element where the text appears, and setting it to pre or something similar. For more information check out the documentation on it on w3schools.

What's the trick to display a text from MySql/PHP back to its original HTML layout?

It still keeps the original text layout (I mean the spacing, offsets, new line, paragraphs) while the text fragment is stored in MySql ('text' type) field - I can tell when I peer into it in my DB browser (Adminer:)
but it gets lost when I output it from the DB: it becomes a single line string of my text characters. How can one restore it its original layout?
I've tried to reshape the text fragment using the PHP nl2br() function with some success:
it brought back the newline breaks, but the text words positioning is not kept, everything
shifts to the left.
Thanks in advance for a good idea.
If you've got multiple spaces and things like that. e.g. for code. Then trying using the pre tag.
http://htmldog.com/reference/htmltags/pre
http://reference.sitepoint.com/html/pre
The html_entity_decode() function converts HTML entities to characters.
The syntax is:
html_entity_decode(string, [quotestyle], [character-set]);
You can refer example2.

how to remove html tags in php?

i posted some data using tinymce (in a symfony project).while retrieving back how can i remove html tags? strip_tags not working..
The easies way is to use strip_tags but it's not very reliable. There is a very, very, VERY good project design specifically for this: HTML Purifier.
It battle-hardened, tested and very good. strip_tags is the easy, fast and go way, but it can miss out some malformated html that a browser will actually parse and execute.
Please, don't use regular expression to parse html!
Note that strip_tags returns a new string. It does not modify the original string, i.e:
$html = '<p>Test</p>';
strip_tags($html); // Throws away the result, since you don't assign the return
// value of the function to a variable
$stripped = strip_tags($html);
echo $stripped; // echos 'Test'
Try this:
echo strip_tags($this->getContent(ESC_RAW))
You could use strip_tags:
strip_tags('your text or variable');
It should work in symfony. Make sure that you have done everything correctly.
When using Symfony, be sure to use the getRaw() function otherwise the text cannot be stripped from it's HTML code, for example:
$myText = $sf_data->getRaw('myVarContainingText');
Then use strip_tags() as such:
$myText = strip_tags( $sf_data->getRaw('myVarContainingText') );
strip_tags(); you need to put what ever your $ is wrapped with html. ...........

Categories