Javascript Regular Expression Code to PHP Regular Expressions ? - javascript

I have following java script regular expression which i need to convert to similar php converter.
text = text.replace(/R/g, "ූ");
Can someone help me to convert this into PHP ?

It's very similar:
$text = preg_replace('/R/', "ූ", $text);
Have look at the preg_replace documentation.

its regex counterpart in php will be :
preg_replace( '/R/', 'your replacement string', $text );
$text = the value of 'text' in your javascript code.
However, for simple text replace, regular expressions are expensive. If your problem can't be solved using simple string functions then only use regex.

Related

Translate javascript string replace function to php

I found in this site a very basic javascript function to encode text. Looking at the source code this is the string replacement code:
txtEscape = txtEscape.replace(/%/g,'#');
So the string stackoverflow becomes #73#74#61#63#6B#6F#76#65#72#66#6C#6F#77
I need a function that does the same elementary encryption in php, but I really don't understand what the /%/g does. I think in php the same function would be something like:
str_replace(/%/g,"#","stackoverflow");
But of course the /%/g doesn't work
Replace a character
Indeed, the PHP function is str_replace (there are many functions for replacements). But, the regex expression is not the same :)
See official documentation: http://php.net/manual/en/function.str-replace.php
In your case, you want to replace a letter % by #.
g is a regex flag. And // are delimiter to activate the regex mode :)
The "g" flag indicates that the regular expression should be tested against all possible matches in a string. Without the g flag, it'll only test for the first.
<?php
echo str_replace('%', '#', '%73%74%61%63%6B%6F%76%65%72%66%6C%6F%77');
?>
In PHP, you can use flags with regex: preg_replace & cie.
Escape
See this post: PHP equivalent for javascript escape/unescape
There are two functions stringToHex and hexToString to do what you want :)
Indeed, the site you provided use espace function to code the message:
document.write(unescape(str.replace(/#/g,'%')));

Unescape unicode characters using PHP

I am using an API which encodes some part of the content using JavaScript. That content is visible in the browser, but while I access it using curl in PHP I get plain JavaScript code as there is no JS engine on server. I would like to decode/unescape Unicode characters in PHP, as JavaScript does. Is that possible?
Please find a snippet of plain JavaScript I get as a response below:
eval(unescape("document.write('%u0039%u0032%u0039%u0032%u0034')"));
The snippet code should return 92924
Ok, I found a solution to that problem.
I am now parsing out the content string and then use json_decode function to unescape Unicode characters, but first %u has to be replaced with \u. Here is my code:
$string = "%u0039%u0032%u0039%u0032%u0034";
$unescaped = str_replace("%u","\u", $string);
echo json_decode('"'.$unescaped .'"');
This code would output: 92924

Why is my search() unable to find special characters?

I think this is a character encoding problem. My javascript search fails to identify a string whenever the string contains certain special characters such as ()* parenthesis, asterisks, and numerals.
The javascript is fairly simple. I search a string (str) for the value (val):
n = str.search(val);
The string I search was written to a txt file using PHP...
$scomment = $_POST['order'];
$data = stripslashes("$scomment\n");
$fh = fopen("comments.txt", "a");
fwrite($fh, $data);
fclose($fh);
...then retrieved with PHP...
$search = $_GET["name"];
$comments = file_get_contents('comments.txt');
$array = explode("~",$comments);
foreach($array as $item){if(strstr($item, $search)){
echo $item; } }
...and put into my HTML using AJAX...
xmlhttp.open("GET","focus.php?name="+str,true);
xmlhttp.send();
My HTML is encoded as UTF-8.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
The problem is that the when I search() for strings, special characters are not recognized.
I didn't really see an option to encode the PHP created text, so I assumed it was UTF-8 too. Is that assumption incorrect?
If this is an encoding problem, how do I change the encoding of the txt file written with PHP? If it is caused by the above code, where is the problem?
The search() function takes a regex. If a string is given, it is converted into one. Because of this, any special characters have special meanings, and it will not work as expected. You need to escape special characters.
Syntax
str.search(regexp)
Parameters
regexp
A regular expression object. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).
If you want to search for text, use str.indexOf() instead, unless you NEED a regex. If so, see here on how to escape the string: Is there a RegExp.escape function in Javascript?
Include this function in your code:
function isValid(str){
return !/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str);
}
Hope it helps

How to replace new line with %0D%0A with PHP

I have a JavaScript code that can replace new lines with %0D%0A
I need the same code in PHP.
This is how my JavaScript code looks like:
text = text.replace(/\n\r?/g, '%0D%0A');
I tried with PHP but I am getting only one line without newlines.
You don't need regular expressions for that. Simple string replacement will be enough.
Use str_replace function:
$test = 'Some very long text with multiple lines...';
$newText = str_replace(PHP_EOL, '%0D%0A', $text);
New line character differs in various systems. It's not a good idea to hard-code \n\r. Better solution is to use PHP_EOL constant.
a literal translation of your java code into php could use preg_replace
http://www.php.net/preg_replace
<?php
$text = "some text";
$text = preg_replace("\r\n","%0D%0A",$text);
?>

JavaScript Regular Expression to get words in quotes string

Thanks for all, I met a problem in JavaScript, and the problem is:
I have a string: such as:
{"results":[{"id":"id1","text":"text1"},{"id":"id2","text":"text2"},{"id":"id3","text":"text3"}]}
Then, I want to get the string such as:
id1|text1|id2|text2|id3|text3
So how should I write the Regular Expression?
Thank you so much, and I am a new comer in StackOverFlow!
I have a string
That string is in JSON format, which works very well with JavaScript.
So how should I write the Regular Expression?
You should not write a regular expression at all. If you did, you only had two problems:
.replace(/(^.+?|"\},\{)?"id":"|","text":"|"}[^"]*$/g, "|").slice(1,-1)
Instead, parse the string into an object, and extract the id-text pairs from there:
var result = JSON.parse(string).results.map(function(el) {
return el.id+"|"+el.text;
}).join("|");

Categories

Resources