I am very new to CakePHP and not very familiar with regular expressions.
I need to use regex in CakePHP to check whether a string has a double quotes character, followed immediately by a comma, then followed by another double quotes character: ","
Here is my attempt:
String::tokenize($problem_string, '/",/"');
I tried ($problem_string, ","), but that parsed the string at every place there was a comma. I also tried ($problem_string, "/",/""), with no luck.
This entry suggests using a backslash in front of the double quotes in Java, but maybe that rule doesn't apply for PHP or CakePHP?
How to represent the double quotes character (") in regex?
I feel like this should be an easy problem to figure out, but I've been stumped for quite a while now.
The escape character you're looking for is the backslash not the forward slash, but you don't have to escape double quotes if you use single quote delimiters, so just this: ($problem_string, '/","/')
Update
After reading String::tokenize docs, and not seeing any mention of regex, I think you just want ($problem_string, '","')
Related
I have some legacy code I'm trying to update to run on PHP 5.4:
$row['subject'] = ereg_replace('[\]', '', $row['subject']);
I know I have to replace this with preg_replace, but since I've never used ereg, I'm not sure what this actually does.
Is the equivalent:
preg_replace('/[\\]/'...
or
preg_replace('/\[\\\]/'...
Can anyone shine a light on what the correct replacement should be?
The code you posted replaces the backslashes with nothing and it doesn't match the square brackets; they are used to create a character range that contains only the backslash character and are completely useless on your regex.
The equivalent preg_replace() statement is:
$row['subject'] = preg_replace('/\\\\/', '', $row['subject']);
The regex is /\\/. Using a single backslash (\) produces an error; the backslash is an escape character in regex, it escapes the / making it be interpreted literally and not as the delimiter. The two extra backslashes are needed because the backslash is also an escape character in PHP and it needs to be escaped too.
I guess this was the reason the original coder enclosed the backslash into a range, to circumvent the need for escaping and double escaping.
Using the same trick you can write it with preg_replace() as:
$row['subject'] = preg_replace('/[\]/', '', $row['subject']);
A single backslash is enough. In the regex the backslash escapes are not allowed in character ranges. And in PHP single-quoted strings, the backslash needs to be escaped only if it is the last character from the string.
(I know, the documentation teaches us to double all the backslashes, but, on the other hand, it says that a backslash that does not precede an apostrophe or another backslash in single-quoted strings is interpreted literally.)
Back to the regex, there is a better (faster, cleaner) way to rewrite the above call to ereg_replace(): do not use regex at all. Because all it does is to match (and replace) the backslashes, you don't need to use regex. A simple str_replace() is enough:
$row['subject'] = str_replace('\\', '', $row['subject']);
I have a document that's full of quotes, so like: "this is a quote". Some of those quotes have subclauses in two hyphens like: "this quote - this one right here - has em dashes", and some just have one hyphen like: "this quote has just one thing - a hyphen".
I'm trying to have some regex that matches all of the quotes with two hyphens, but not match any quotes with zero or one hyphen, and not match any of the text outside of the quotes. Also I should mention that there are some sentences with one or more hyphens that lie outside of quotes, I need to ignore them as well and not have them interfere with my matches in quotes. I want to change the properly matched quotes' double hyphens to proper em dash characters.
I've tried using lookaheads and negated characters, but can't seem to figure this one out.
Is this something regex can do, or do I need to come up with some kind of other approach (like splitting all of the text into an array and stepping through it, making my changes and then recombining it all at the end)? I can do that instead it just seems like a silly waste of time if there's a one-line regex statement that will do what I want.
Add a \b word boundary at the beginning of the quote, and check that the last character inside the quote is either a letter or number or some kind of punctuation.
("\b[^-"]*-[^-"]*-[^-"]*[\w.!?]")
"(?:[^-"]*-){2}[^-"]*" is about the best you can get with only regex, but it doesn't work if there are two hyphens outside of quotes. Splitting the text into an array is probably the best way to do what you want to.
The string quotes included.
'test.test.test.test003232'
The reg ex I have so far
^test.test.test.test[0-99999]$
If I removed the quotes in the string, then the regular expression works, but I need to escape the quotes. I tried backslashes
^\'test.test.test.test[0-99999]\'$
but it didn't seem to work. Can anyone help me with this problem.
You must escape the ., because it will match any character. There is no reason to escape ', unless you wrap it in quotes in your PHP, which you didn't provide. [0-999999] doesn't make sense, I'll assume you want 6 digits.
Also assuming that you are using preg_match, you need delimiters.
It should be:
/^'test\.test\.test\.test[0-9]{6}'$/
Or inside PHP code:
$pattern = "/^'test\.test\.test\.test[0-9]{6}'$/";
Your regex is wrong, try this:
^\'test\.test\.test\.test([0-9]+)\'$
I have double backslashes '\' in my string that needs to be converted into single backslashes '\'. I've tried several combinations and end up with the whole string disappearing when I used echo or more backslashes are added to the string by accident. This regex thing is making me go bonkers...lol...
I tried this amongst other failed attempts:
$pattern = '[\\]';
$replacement = '/\/';
?>
<td width="100%"> <?php echo preg_replace($pattern, $replacement,$q[$i]);?></td>
I do apologise if this is a foolish issue and I appreciate any pointers.
Use stripslashes() - it does exactly what you're looking for.
<td width="100%"> <?php echo stripslashes($q[$i]);?></td>
Use stripslashes instead. Also, in your regex, you are searching for single backslashes and your replacement is incorrect. \\{2} should search for double backslashes and \ should replace them with singles, although I haven't tested this.
Just to explain further, the pattern [\\] matches any character in a set comprised of a single backslash. In php, you should also delimit your regex with forward slashes: /[\\]/
Your replacement, which is (without delimiters) \, is not a regular expression for matching a single backslash. The regex for matching a single backslash is \\. Note the escaping. This said, the replacement term needs to be a string, not a regex (with the exception of backreferences).
EDIT: Sven claims below that stripslashes removes all backslashes. This is simply not true, and I will explain why below.
If a string contains 2 backslashes, the first one will be considered an escaping backslash and will be removed. This can be seen at http://www.phpfiddle.org/main/code/3yn-2ut. The fact that any backslashes remain at all by itself contradicts the claim that stripslashes removes all backslashes.
Just to clarify, this string declaration is invalid: $x = "\";, since the backslash escapes the second quote. This string "\\" contains one backslash. In the process of unquoting this string, this backslash will be removed. This "\\\\" string contains two backslashes. When unquoting, the first will be considered an escaping backslash, and will be removed.
Use preg_replace to turn double backslash into single backslash:
preg_replace('/\\\\{2}/', '\\', $str)
The \ in the first parameter needs to be escaped twice, once for string and once more for regex, just like CodeAngry says.
In the second parameter it only gets excaped once for string.
Make sense?
Never use a regular expression if the string you are looking for is constant, as is the case with "Every instance of double backslash".
Use str_replace() for this task. It is a very easy function that replaces every occurance of a string with another.
In your case: str_replace('\\\\', '\\', $var).
The double backslash actually translates into four backslashed, because inside any quotes (single or double), a single backslash is the start of an escape sequence for the following character. If you want one literal backslash, you have to write two of them. You want two backslashes, you have to write four of them.
I do not like the suggestion of stripslashes(). This will of course "decode" your double backslash into one single backslash. But it will also remove all single backslashes in the whole string. If there were none - fine, otherwise things will fail now.
$pattern = '[\\]'; // wrong
$pattern = '[\\\\]'; // right
escape \ as \\ and escape \\ as \\\\ because \\] means escaped ].
Use htmlentities function to convert your slashes to html entities then using str_replace or preg_match to change them with new entity
I am using xampp server to learn php and mysql, and I noticed that (\n) for a new line doesn't work?
Does anyone noticed this before in xampp?
This applies to \t,\r too.
From the PHP Manual:
The simplest way to specify a string is to enclose it in single quotes
(the character ').
To specify a literal single quote, escape it with a backslash (\). To
specify a literal backslash, double it (\\). All other instances of
backslash will be treated as a literal backslash: this means that the
other escape sequences you might be used to, such as \r or \n, will be
output literally as specified rather than having any special meaning.
So you have to use double quotes to get \n, \t and other escaped characters (you can find a list on the linked manual page).
Your problem is not specific to XAMPP and is a general PHP thing.
They work in xampp.
You need to use double quotes: "\n"