Escape Characters In both types of string: In ” ” strings: Note, using say ‘\n’ will not create a newline character, it will create the characters \n !!!!! \n only works in a “” string!! Joining strings Use the period character ‘.’ You can also use .= Is String Empty Length ‘ vs “ ‘ […]
Category: Strings
Comma Separated Strings
Convert comma separated string to array and test for entries Convert array into comma separated string
Compare Strings
== vs === – IMPORTANT FOR STRINGS!!!!! When comparing a string to an integer the string is evaluated as 0!! evaluates true because first “ABC” is converted to integer and becomes 0 then it is compared to 0. Use === if you are comparing non strings to a string value
Convert Strings
Converting to HTML, from form POST, etc See here Converting String To Variable You don’t need to, but if you want to force to a particular type you can: Converting Variables To String No need, PHP automatically converts a varaible to the type needed for the context being used Convert copy and pasted text Remove […]
Convert Strings to Array
Convert comma separated string into array See dedicated page here. Convert array to comma separated string See dedicated page here. Convert string to byte array json encode See here
Converting Form Text Fields
filter_var() function Returns the input string filtered into the required typ, or FALSE if it was unable to perform the sanitization (e.g. due to illegal characters etc) See here for all the available filter options. Example – INT Sanitising for HTML from a form POST If server magic quotes is turned on then it will add / […]
Heredoc string
You can use the <<< sequence like this (this is called a Heredoc string): The terminating marker must be at the start of a line and the only thing on the line (no comment or even whitespace after it!!!) The advantage of this is that there is no need for line breaks, escape characters (\’ […]
Import file into a string
Import an html file into a string trim(file_get_contents($_SERVER[‘DOCUMENT_ROOT’] . '/email_templates/membership_request_subject.htm'));
Limit string length
If string too long then trim it Remove last # characters