I have got the following issue, my function appends code to a string $string ($string .= bla), but in the beginning the string is empty, so I get this error message A PHP Error was encountered Severity: Notice Message: Undefined variable: $string Filename: libraries/file.php Line Number: 90 Of course if I define the string in advance, like $string = NULL,
Tag: string
Add spaces between words in a camelCased string then uppercase the first word
I have two types of strings, hello and helloThere. What I want is to change them so they read like: Hello and Hello There depending on the case. What would be a good way of doing this? Answer you can use ucwords like everyone said… to add the space in helloThere you can do $with_space = preg_replace(‘/[A-Z]/’,” $0″,$string); then ucwords($with_space);
Replacing accented characters php
I am trying to replace accented characters with the normal replacements. Below is what I am currently doing. $string = “Éric Cantona”; $strict = strtolower($string); echo “After Lower: “….
How to remove numbers from a string with RegEx
I have a string like this: I would like to remove 23 so I’m left with PM or (with space truncated) just PM. Any suggestions? Needs to be in PHP Answer
Replacing a specific part of a query string PHP
I use $_SERVER[‘QUERY_STRING’] to get the query sting. A example would be a=123&b=456&c=789 How could I remove the b value from the query string to obtain a=123&c=789 where b can be any value of any length and is alpha numeric. Any ideas appreciated, thanks. Answer The value is going to be $_GET[‘b’]. How about:
Truncate a string to first n characters of a string and add three dots if any characters are removed
How can I get the first n characters of a string in PHP? What’s the fastest way to trim a string to a specific number of characters, and append ‘…’ if needed? Answer Update: Based on suggestion for checking length (and also ensuring similar lengths on trimmed and untrimmed strings): So you will get a string of max 13 characters;
Strip all non-alphanumeric, spaces and punctuation symbols from a string
How can I use PHP to strip out all characters that are NOT letters, numbers, spaces, or punctuation marks? I’ve tried the following, but it strips punctuation. Answer Example: p{P} matches all Unicode punctuation characters (see Unicode character properties). If you only want to allow specific punctuation, simply add them to the negated character class. E.g:
How to prefix a positive number with plus sign in PHP
I need to design a function to return negative numbers unchanged but should add a + sign at the start of the number if its already no present. Example: It will get only numeric input. This function is going to be called several times in echo/print so the quicker the better. Answer You can use regex as: But I would
How to get the first word of a sentence in PHP?
I want to extract the first word of a variable from a string. For example, take this input: The resultant output should be Test, which is the first word of the input. How can I do this? Answer You can use the explode function as follows: Another example:
How can I replace strings NOT within a link tag?
I am working on this PHP function. The idea is to wrap certain words occuring in a string into certain tags (both, words and tags, given in an array). It works OK!, but when those words occur into a …