So I've got this piece of code that wont play nice.
preg_match_all("/(\{\[)([\w-\d\s\.\|']*)(\]\})/i",$replace_text, $match);
What it is supposed to do, is allow an apostrophe to be in my replacement text. So in my text, where i have "{[SPIN--they are|they’re]}" it should return "they are" or "they're".
But instead, it simply does nothing and spits out the entire spintax code just as I typed above.
The only time this does not work, is when a replacement text has an apostrophe. It works perfectly everywhere else. Been trying to fix this for two days and I'm about to throw my keyboard through my monitor.
There are many things that my project does and it is imperative to have the {[SPIN-- before specifying the replacement text, and the ]} closing brackets.
Can someone help, please?
In your example string it's not a single quote character, but something that looks similarly
’ (the actual character) vs ' (that's what you think it is)
Related
EDIT: I found a solution I didn't expect. See below.
Using regex via PHP's preg_match_all , I want to match a certain url (EDIT: that is already escaped) in a string formatted as json. The search works wonderfully in Notepad++ (using regex-matching, of course) but preg_match_all() just returns an empty array.
Testing on tryphpregex.com I found out that somehow my usual approach to escaping a backslash gives a pattern error, i.e. even the simple pattern https:\\ returns an empty result.
I'm utterly confused and have been trying to debug for too long so I may miss the obvious. Maybe one of you can see the simple error?
The string.
The pattern (that works fine in Notepad++, but not in PHP):
%(https:\\/\\/play.spotify.com\\/track\\/)(.*?)(\")%
You don't need to escape the slash in PHP %(https://play.spotify.com/track/)(.*?)(\")%
The Backslash before doule quote is only needed if you enclosures are double quotes too.
Found a solution to my problem.
According to this site, I need to match every backslash with \\\\. Horrible, but true.
So my pattern becomes:
$pattern = "%(https:\\\\/\\\\/play\.spotify\.com\\\\/track\\\\/)(.*?)(\")%";
Please observe that I tried to find a pattern inside a string that didn't contain clear urls, but urls containing escape characters (it was a json-output from spotify)
Is there a PHP function that can take a string and convert any special characters to unicode. Similar to htmlspecialchars() or UTF8_encode().
For example in the string: "I think Bob's going too".
I would need the apostrophe or single right quote unicode in place of the apostrophe in "Bob's". So then after conversion the string should read: "I think Bob\u2019s going too".
I need this for use in a PHP script that prints into a javascript function.
Using \ to escape or ' does not work, it stops the script from running. I am trying to use Flowplayers Playist plugin. The only way it seems I can have a string with special characters is if they are in unicode.
Here is a JSFIDDLE to play around with and see what I mean when I say it doesn't work. Just replace \u2019 with ' or something similar and click to have the song play. The media player just goes black and doesn't play anything, whereas if you leave it with \u2019 then it plays fine.
Any help is appreciated.
I think json_encode() is the function you are looking for here.
The following code:
$string = "I think Bob’s going too";
print_r(json_encode($string));
will output:
"I think Bob\u2019s going too"
I have searched in vain to find a fix for this issue. I have an editable field in a web page that contains a user entered space. When I copy the space and enter it into a program called IVI32 which I guess you would call a Unicode text program, I get the following info.
The space character is defined as FFFE2000. I need to detect when this field has one or more of these spaces and nothing else. I have tried the following with preg_match:
'/\s+/u'
'/^[0 :-]+$/ '
'/\A\s*\z/'
Nothing works and I am completely stumped. Any help from some Unicode experts out there will be greatly appreciated.
There was an error in my code which was preventing anything from working properly (the product of no time off!). Here's what works for anyone else who might want to detect if an element contains only whitespace that cannot be eliminated by php trim();
if(!preg_match('/\\s/', $test_string)):-do something-
if(!preg_match('/\s+/u', $test_string)):-do something-
if(!preg_match('/[\pZ\pC]+/u', $test_string)):-do something-
For anyone who is interested the space is pasted immediately after the end of this sentence.
Would this work?
preg_match('/^ +$/', $subject);
Match a single or more spaces? Because \s will also match nonbreaking spaces, tabs, and newlines.
Have a try with:
/\p{WhiteSpace}/
my code is not working ? and i dont want to use str_replace , for there maybe more slashes than 3 to be replaced. how can i do the job using preg_replace?
my code here like this:
<?php
$str='<li>
<span class=\"highlight\">Color</span>
Can\\\'t find the exact color shown on the model pictures? Just leave a message (eg: color as shown in the first picture...) when you place order.
Please note that colors on your computer monitor may differ slightly from actual product colors depending on your monitor settings.
</li>';
$str=preg_replace("#\\+#","\\",$str);
echo $str;
There is merit in the other answers, but to me it looks like what you're actually trying to accomplish is something very different. In the php code \\\' is not three slashes followed by an apostrophe, it's one escaped slash followed by an escaped apostrophe, and in the rendered output, that's exactly what you see—a slash followed by an apostrophe (with no need to escape them in the rendered html). It's important to realize that the escape character is not actually part of the string; it's merely a way to help you represent a character that normally has very different meaning in within php—in this case, an apostrophe normally terminates a string literal. What looks like 4 characters in php is actually only 2 characters in the string.
If this is the extent of your code, there's no need for string manipulation or regular expressions. What you actually need is just this:
<?php
$str='<li>
<span class="highlight">Color</span>
Can\'t find the exact color shown on the model pictures? Just leave a message (eg: color as shown in the first picture...) when you place order.
Please note that colors on your computer monitor may differ slightly from actual product colors depending on your monitor settings.
</li>';
echo $str;
?>
Only one escape character is needed here for the apostrophe, and in the rendered HTML you will see no slashes at all.
Further Reading:
Escape sequences
The root of this problem is actually in how it was written into your database and likely to be caused by magic_quotes_gpc; this was used in older versions and a really bad idea.
The best fix
This requires a few steps:
Fix the script that puts the HTML inside your database by disabling magic_quotes_gpc.
Write a script that reads all existing database entries, applies stripslashes() and saves the changes.
Fix the presentation part (though, that may need no changes at all.
Alternative patch
Use stripslashes() before you present the HTML.
use this pattern
preg_replace('#\\+#', '\\', $text);
This replaces two or more \ symbols preceding an ' symbol with \'
$theConvertedString = preg_replace("/\\{2,}'/", "\'", $theSourceString);
Ideally, you shouldn't have code causing this issue in the first place so I would have a look at why you have \\' in your code to begin with. If you've manually put it in your variables, take it out. Often, this also happens with multiple calls to addslashes() or mysql_real_escape_string() or a cheap hosting providers' automatic transformation of all POST request variables to escape slashes, combined with your server side PHP code to do the same.
I've asked this question before but I didn't seem to get the right answer. I've got a problem with new lines in text. Javascript and jQuery don't like things like this:
alert('text
text);
When I pull information from a database table that has a break line in it, JS and jQuery can't parse it correctly. I've been told to use n2lbr(), but that doesn't work when someone uses 'shift+enter' or 'enter' when typing text into a message (which is where I get this problem). I still end up with separate lines when using it. It seems to correctly apply the BR tag after the line break, but it still leaves the break there.
Can anyone provide some help here? I get the message data with jQuery and send it off to PHP file to storage, so I'd like to fix the problem there.
This wouldn't be a problem normally, but I want to pull all of a users messages when they first load up their inbox and then display it to them via jQuery when they select a certain message.
You could use a regexp to replace newlines with spaces:
alert('<?php preg_replace("/[\n\r\f]+/m","<br />", $text); ?>');
The m modifier will match across newlines, which in this case I think is important.
edit: sorry, didn't realise you actually wanted <br /> elements, not spaces. updated answer accordingly.
edit2: like #LainIwakura, I made a mistake in my regexp, partly due to the previous edit. my new regexp only replaces CR/NL/LF characters, not any whitespace character (\s). note there are a bunch of unicode linebreak characters that i haven't acknowledged... if you need to deal with these, you might want to read up on the regexp syntax for unicode
Edit: Okay after much tripping over myself I believe you want this:
$str = preg_replace('/\n+/', '<br />', $str);
And with that I'm going to bed...too late to be answering questions.
I usually use json_encode() to format string for use in JavaScript, as it does everything that's necessary for making JS-valid value.