How do I feed string variable holding multiline value in it to jquery method like prepend? I need it to construct a form pass by drupal search module like - $('#divsearch').prepend('<?php print $search_box; ?>'). Thanks a bunch.
Use json_encode() to encode the string, like this:
$('#divsearch').prepend('<?php print json_encode($search_box); ?>');
This will encode newlines as \n instead of literally rendering them...giving you your current syntax errors.
I think the problem is that you can have carriage returns or new lines in you string lets take out the php and show what i mean
$('#divsearch').prepend('<div />');
this would work
$('#divsearch').prepend('<div>some text that goes
in a div</div>');
this does not work
$('#divsearch').prepend('<div>some text that goes'+
'in a div</div>');
this woould
Related
So. I have three massive texts from WYSIWYG editor that I need to store in DB (MySQL). To do that I use PHP function rawurlencode() put them in one array and serialize() the array, then insert into DB. But the problem is when I need to rawurldecode().
First I unserialize() then text decodes normally, but image tag decodes like this:
`<img alt="\"\"" src="\"/ckfinder/userfiles/images/facebook.PNG\""
style="\"float:right;" height:200px; width:198px\">`
there is multiple these ->\" that I dont need and in fact they don't appear in code that is not encoded. str_replace() won't help.
Maybe I do something wrong and there is better way how to store long texts in serialized array?
Use the stripcslashes() function available by default in PHP.
It will remove the backslashes that are added as escape characters.
Example
I am trying to add a user-defined string to information passed to a third party via href. So I have something that will look like
Link Text
USERSTRING is known when the page loads so it could be put in the href by php when the page loads, or I can dynamically add it with javascript.
What I don't know is what I need to do to escape any special characters so that the link works and can be read on the other end - USERSTRING could be something really annoying like: [He said, "90% isn't good enough?"] The data is only used in an auto-generated file name so it doesn't need to be preserved 100%, but I'm trying to avoid gratuitous ugliness.
The urlencode() function provides exactly what you are looking for, ie:
Link Text
You need to urlencode it. If the variant of urlencode you end up using doesn't encode '&', '#', '"', and angle brackets as it should then you'll need to HTML encode it too.
I am passing a string variable from php to javascript.
The string contains "
But javascript doesn't get it.
How can I escape this character?
UPD:
To be more clear, first I don't want to make many changes in the code (not written by me)...
The string is passed this way:
var string = '<? echo $string;?>' ;
Single quotes are used. Maybe there is a way to change smth. in the string itself?
You could use the json_encode method:
<script type="text/javascript">
var value = <?php echo json_encode($someValue); ?>;
alert(value);
</script>
Assuming a string delimited using double quotes, add_slashes will do the job in the particular case.
Wrapping the data in an associative array, running it through json_encode and altering the JS to expect the changed data structure is a safer approach though (since that will take care of other characters which are significant, such as literal new lines).
(Technically speaking, with the current implementation of json_encode you could skip wrapping it in an associative array … but a plain string isn't valid JSON and I'm inclined to avoid depending on a function that is supposed to generate JSON not throwing an exception when given a data structure that can't be turned into JSON).
If you are embedding the script in an HTML document you will also have to take steps to ensure that the resulting JS doesn't contain any HTML that could cause issues (such as " in an script included as an attribute value).
Use urlencode() function in php code to pass the string to javascript code and decodeuri() in javascript to decode that string.
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.
I'm using PHP to create some basic HTML. The tags are always the same, but the actual links/titles correspond to PHP variables:
$string = '<p style="..."><strong><i>'.$title[$i].'</i></strong>
<br>';
echo $string;
fwrite($outfile, $string);
The resultant html, both as echoed (when I view the page source) and in the simple txt file I'm writing to, reads as follows:
<p style="..."><a href="http://www.example.com
"><strong><i>Example Title
</i></strong></a></p>
<br>
While this works, it's not exactly what I want. It looks like PHP is adding a line break every time I interrupt the string to insert a variable. Is there a way to prevent this behavior?
Whilst it won't affect your HTML page at all with the line breaks (unless you are using pre or text-wrap: pre), you should be able to call trim() on those variables to remove newlines.
To find out if your variable has a newline at front or back, try this regex
var_dump(preg_match('/^\n|\n$/', $variable));
(I think you have to use single quotes so PHP doesn't turn your \n into a literal newline in the string).
My guess is your variables are to blame. You might try cleaning them up with trim: http://us2.php.net/trim.
The line breaks show up because of multi-byte encoding, I believe. Try:
$newstring = mb_substr($string_w_line_break,[start],[length],'UTF-8');
That worked for me when strange line breaks showed up after parsing html.