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
- Replace all the double quotes in your javascript with single quotes
- Escape all the quotes as Andreas mentioned
- 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
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!»
?php
$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.?php>
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.
- Operators
- Operator Precedence
- Arithmetic Operators
- Assignment Operators
- Bitwise Operators
- Comparison Operators
- Error Control Operators
- Execution Operators
- Incrementing/Decrementing Operators
- Logical Operators
- String Operators
- Array Operators
- Type Operators
Add text to string php
- Different ways to write a PHP code
- How to write comments in PHP ?
- Introduction to Codeignitor (PHP)
- How to echo HTML in PHP ?
- Error handling in PHP
- How to show All Errors in PHP ?
- How to Start and Stop a Timer in PHP ?
- How to create default function parameter in PHP?
- How to check if mod_rewrite is enabled in PHP ?
- Web Scraping in PHP Using Simple HTML DOM Parser
- How to pass form variables from one page to other page in PHP ?
- How to display logged in user information in PHP ?
- How to find out where a function is defined using PHP ?
- How to Get $_POST from multiple check-boxes ?
- How to Secure hash and salt for PHP passwords ?
- Program to Insert new item in array on any position in PHP
- PHP append one array to another
- How to delete an Element From an Array in PHP ?
- How to print all the values of an array in PHP ?
- How to perform Array Delete by Value Not Key in PHP ?
- Removing Array Element and Re-Indexing in PHP
- How to count all array elements in PHP ?
- How to insert an item at the beginning of an array in PHP ?
- PHP Check if two arrays contain same elements
- Merge two arrays keeping original keys in PHP
- PHP program to find the maximum and the minimum in array
- How to check a key exists in an array in PHP ?
- PHP | Second most frequent element in an array
- Sort array of objects by object fields in PHP
- PHP | Sort array of strings in natural and standard orders
- How to pass PHP Variables by reference ?
- How to format Phone Numbers in PHP ?
- How to use php serialize() and unserialize() Function
- Implementing callback in PHP
- PHP | Merging two or more arrays using array_merge()
- PHP program to print an arithmetic progression series using inbuilt functions
- How to prevent SQL Injection in PHP ?
- How to extract the user name from the email ID using PHP ?
- How to count rows in MySQL table in PHP ?
- How to parse a CSV File in PHP ?
- How to generate simple random password from a given string using PHP ?
- How to upload images in MySQL using PHP PDO ?
- How to check foreach Loop Key Value in PHP ?
- How to properly Format a Number With Leading Zeros in PHP ?
- How to get a File Extension in PHP ?
- How to get the current Date and Time in PHP ?
- PHP program to change date format
- How to convert DateTime to String using PHP ?
- How to get Time Difference in Minutes in PHP ?
- Return all dates between two dates in an array in PHP
- Sort an array of dates in PHP
- How to get the time of the last modification of the current page in PHP?
- How to convert a Date into Timestamp using PHP ?
- How to add 24 hours to a unix timestamp in php?
- Sort a multidimensional array by date element in PHP
- Convert timestamp to readable date/time in PHP
- PHP | Number of week days between two dates
- PHP | Converting string to Date and DateTime
- How to get last day of a month from date in PHP ?
- PHP | Change strings in an array to uppercase
- How to convert first character of all the words uppercase using PHP ?
- How to get the last character of a string in PHP ?
- How to convert uppercase string to lowercase using PHP ?
- How to extract Numbers From a String in PHP ?
- How to replace String in PHP ?
- How to Encrypt and Decrypt a PHP String ?
- How to display string values within a table using PHP ?
- How to write Multi-Line Strings in PHP ?
- How to check if a String Contains a Substring in PHP ?
- How to append a string in PHP ?
- How to remove white spaces only beginning/end of a string using PHP ?
- How to Remove Special Character from String in PHP ?
- How to create a string by joining the array elements using PHP ?
- How to prepend a string in PHP ?