Terminal

Add text to string php

I am using fwrite to add javascript to a external script. In order to do this there needs to be a php string with the java inside. However the » and ‘ get mixed up and I am not sure how to fix it. Here Is the code:

$page = "  

The Script does what I want it to do, however the speech marks within the javascript mix with the php string speech marks as you can see above, causing the php to throw an error. Is there a way to fix this?

$page = "  ";
$page = . enter the rest of the code here. EOF; echo $page; 

find a manual to learn how to use heredoc

  1. Replace all the double quotes in your javascript with single quotes
  2. Escape all the quotes as Andreas mentioned
  3. Use heredoc

Personally, I would go for #3

Use «»» , or ‘»»‘ , or «\»\» » to escape the strings.

Concatenation of two strings in PHP, There are two string operators. The first is the concatenation operator (‘.’), which returns the concatenation of its right and left

How to add more text to a PHP string

term

I making a PHP script which is powered by a POST request mainly, and it shows an HTML form which is like a command prompt, how can I simulate the text adding? I mean something like this (Mac OS X Terminal):

     "; $prompt .= "

Today is: ".date("M d")." of ".date("o")."

www@".$_SERVER['SERVER_NAME'].":~$>

"; $commandexp = explode(" ", $command); if($commandexp[0] === "echo") < $prompt .= $prompt.$commandexp[1].$prompt; echo $prompt; >else < echo "command not found"; >?>

Add a textbox field with no border and transparent background at the end of your command lines

and when user hits «enter», it sends the post request and refresh the list.

Pass a PHP string to a JavaScript variable (and escape newlines), You can insert it into a hidden DIV, then assign the innerHTML of the DIV to your JavaScript variable. You

Javascript variable passing text string with apostrophe, get cut off?

I have a block of code that contains this line in a form:

On the receiving end, I have a document that grabs the data like so:

This is being inserted into a database, but any «title» that contains an apostrophe is getting cut off at the apostrophe — see example:

«I Don’t Want It» ——> results in «I Don»

«David’d Car» ——> results in «David»

I did try several php str_replace efforts on the processing side with no luck, such as:

$title = str_replace(''', '"', $_POST['title']); 

It appears that the data is not being sent properly, but not sure, Any Suggestions

THIS IS THE FULL BLOCK AND INSERT BELOW THAT:

$active = $_POST['active']; $mediatype = $_POST['mediatype']; $title = $_POST['title']; // $title = str_replace(''', '"', $_POST['title']); $artist = $_POST['artist']; $source_url = $_POST['source_url']; $playlists = $_POST['playlists']; $user = $_POST['user']; $playlist = $_POST['playlist']; $rec_insert = mysql_query("INSERT INTO member_tracks(active, mediatype, title, artist, source_url, playlists, user) VALUES ('$active', '$mediatype', '$title', '$artist', '$source_url', '$playlists', '$user')"); if(! $rec_insert ) < die('Could not enter data: ' . mysql_error()); >else < echo ''; echo ''; echo ''; echo ''; echo ''; echo '
'; echo '

TRACK ADDED SUCCESSFULLY

'; echo '
'; echo ''; echo ''; > // $conn->close();

    First the immediate problem of placing the title in the input element via some jQuery code. You currently build a string that is made up like this:

As you already detected, a quote in the title value will break things. You can escape the title by using this function:

function escapeHtmlAttribute(s) < return s.replace(/&/g, "&").replace(/'/g, "'"); >

This encodes the quotes with the appropriate HTML entity, and also escapes the ampersand as otherwise it could be misunderstood as the start of an HTML entity. So then your string would be built like this:

"INSERT INTO . VALUES(. '" + mysql_real_escape_string($title) + "', . )" 

You can try with Template Literals:

How to prepend a string in PHP ?, Method 1: Using Concatenation Operator(“.”): The Concatenation operator is used to prepend a string str1 with another string str2 by

Источник

Add text to string php

There are two string operators. The first is the concatenation operator (‘.’), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator (‘ .= ‘), which appends the argument on the right side to the argument on the left side. Please read Assignment Operators for more information.

$a = «Hello » ;
$b = $a . «World!» ; // now $b contains «Hello World!»

$a = «Hello » ;
$a .= «World!» ; // now $a contains «Hello World!»
?>

See Also

User Contributed Notes 6 notes

As for me, curly braces serve good substitution for concatenation, and they are quicker to type and code looks cleaner. Remember to use double quotes (» «) as their content is parced by php, because in single quotes (‘ ‘) you’ll get litaral name of variable provided:

// This works:
echo «qwe < $a >rty» ; // qwe12345rty, using braces
echo «qwe» . $a . «rty» ; // qwe12345rty, concatenation used

// Does not work:
echo ‘qwerty’ ; // qwerty, single quotes are not parsed
echo «qwe $arty » ; // qwe, because $a became $arty, which is undefined

A word of caution — the dot operator has the same precedence as + and -, which can yield unexpected results.

The above will print out «3» instead of «Result: 6», since first the string «Result3» is created and this is then added to 3 yielding 3, non-empty non-numeric strings being converted to 0.

To print «Result: 6», use parantheses to alter precedence:

» < $str1 >< $str2 > < $str3 >» ; // one concat = fast
$str1 . $str2 . $str3 ; // two concats = slow
?>
Use double quotes to concat more than two strings instead of multiple ‘.’ operators. PHP is forced to re-concatenate with every ‘.’ operator.

If you attempt to add numbers with a concatenation operator, your result will be the result of those numbers as strings.

echo «thr» . «ee» ; //prints the string «three»
echo «twe» . «lve» ; //prints the string «twelve»
echo 1 . 2 ; //prints the string «12»
echo 1.2 ; //prints the number 1.2
echo 1 + 2 ; //prints the number 3

Some bitwise operators (the and, or, xor and not operators: & | ^ ~ ) also work with strings too since PHP4, so you don’t have to loop through strings and do chr(ord($s[i])) like things.

See the documentation of the bitwise operators: https://www.php.net/operators.bitwise

Be careful so that you don’t type «.» instead of «;» at the end of a line.

It took me more than 30 minutes to debug a long script because of something like this:

The output is «axbc», because of the dot on the first line.

Оцените статью