I have $_SERVER['DOCUMENT_ROOT'] echoed, the problem is when it is echoed from xampp it shows, like this:
C:/xampp/hdocs
and when echoed from PHPed it shows
C:\\xampp\\htdocs.
I want to replace "C:\\xampp\\htdocs" with "C:/xampp/hdocs" but I'm not able to do so using preg_replace.
Kindly help me !
You don't need regex for that. Use the str_replace() fnction:
str_replace('\\', '/', $str);
Related
I am trying to make a multi preg_replace, not sure if thats the correct function.
I want this outcome
[link]www.mynewhomepage.com(My new homepage)[/link]
to become <a href=mynewhomepage.com>My New homepage</a>
I have made this code, which dosent give me what i want
$string = 'i have made a new homepage visit [link]http://myhomepage.dk(My New homepage)[/link]';
$find = array('#\[link\](.+)\[\/link\]#iUs', '#\((.+)\)#iUs');
$replace = array('<a href=$1>', '</a>');
$result = preg_replace($find, $replace, $string);
echo $result;
And it give me this outcome: http://myhomepage.dk>
Can anyone guide me or help me in the right direction of what i am doing wrong? :)
Thanks and happy summer for you :)
This should work:
\[link\](.*?)\((.*)\)\[\/link\]
https://regexr.com/3sc23
It basically matches up to first left parenthesis and also from last parenthesis to the end. Then you put these pieces into capturing groups for referencing them later.
Use $2 as substitution
About your original question, your solution had these problems:
For first replacement we should use <a href="$1. Note that we use " at the beginning of the link but for the moment we do not add it at the end. That way, the nest regexp will be easier.
Then, on the second regexp we should add "> at the beginning in order to close the tag. Also you were not using the captured group at all. That would be the replacement: ">$1</a>
That is, change this line:
$replace = array('<a href=$1>', '</a>');
to this:
$replace = array('$1');
I have $stringF. Contained within $stringF is the following (the string is all one line, not word-wrapped as below):
http://news.google.com/news/url?sa=t&fd=R&ct2=us&usg=
AFQjCNHWQk0M4bZi9xYO4OY4ZiDqYVt2SA&clid=
c3a7d30bb8a4878e06b80cf16b898331&cid=52779892300270&ei=
H4IAW6CbK5WGhQH7s5SQAg&url=https://abcnews.
go.com/Lifestyle/wireStory/latest-royal-wedding-thousands-streets-windsor-55280649
I want to locate that string and make it look like this:
https://abcnews.go.com/Lifestyle/wireStory/latest-royal-
wedding-thousands-streets-windsor-55280649
Basically I need to use preg_replace to find the following string:
http://news.google.com/news/url?sa= ***SOME UNKNOWN CONTENT*** &url=http
and replace it with the following string:
http
I'm a little rusty with my php, and even rustier with regular expressions, so I'm struggling to figure this one out. My code looks like this:
$stringG = preg_replace('http://news.google.com/news/url?sa=*&url=http','http',$stringH);
except I know I can't use wildcards and I know I need to specially deal with the special characters (colon, forward slash, question mark, and sign, etc). Hoping someone can help me out here.
Also of note is that my $stringF contains multiple instances of such strings, so I need the preg_replace to be not greedy - otherwise it will replace a huge chunk of my string unnecessarily.
PHP has tools for that, no need to use a regex. parse_url to get the components of an url (scheme, host, path, anchor, query, ...) and parse_str to get the keys/values of the query part.
$url = 'http://news.google.com/news/url?sa=t&fd=R&ct2=us&usg=AFQjCNHWQk0M4bZi9xYO4OY4ZiDqYVt2SA&clid=c3a7d30bb8a4878e06b80cf16b898331&ci=52779892300270&ei=H4IAW6CbK5WGhQH7s5SQAg&url=https://abcnews.go.com/Lifestyle/wireStory/latest-royal-wedding-thousands-streets-windsor-55280649';
parse_str(parse_url($url, PHP_URL_QUERY), $arr);
echo $arr['url'];
Why I cannot use str_replace() to replace content between the ""? While I replace links within a file they get skipped since they are within quotes.
Example.
href="/path/to/file/is/here"
should be
href="/New/Path/To/File/Goes/Here"
If the paths/urls were not in quotes, str_replace() would work.
I'm assuming this is PHP. So, from the examples here:
http://php.net/manual/en/function.str-replace.php
You can see that you should not intercalate the same type of quotes.
So try changing the quotes in your code to single quotes or, change, the double quotes in your html to single quotes.
If that's not it, I hope at least that doc reference helps you.
This might help I usually code in java but php is pretty similar. Next time input part of your code so that the community can see your logic.
In your if statement on line 67 the 3rd variable $stringToSearch should be regex not the string your assigning it to. The purpose of regex as you know is to replace characters you don't want in your code as you already know
What you had that was not working:
// replacing string from files
//$stringToSearch = str_replace('"', "!!", $stringToSearch);
$stringToSearch = str_replace($toBeReplaced, $toBeReplacedWith, $stringToSearch);
//$stringToSearch = str_replace("!!", '"', $stringToSearch);
What I am thinking it should be:
$stringToRegex = str_replace('"', "!!", $stringToSearch);
$stringToSearch = str_replace($toBeReplaced, $toBeReplacedWith, $stringToRegex );
If anyone else has any suggestion it would be appreciated as i don't code in php.
I want to define a variable /Test123 in perl .I am trying something like this but i am getting error
my $uriReference="Test123";
##Here is where i am trying to create /Test123
my $routingUrl = "\"/".$uriReference."\"";
I think i am doing some mistake here Need help?
You can use qq() instead to emulate double quote interpolation:
my $url = qq("/$uriReference");
Although I do not see anything technically wrong with your assignment, it should assign "/Test123" to your variable. So perhaps the error is about something else. We would know if you had included your error message in your question. There is no reason you should ever not include error messages in questions.
You should also know that you do not need to break up your quotes, you can do this:
my $url = "\"/$uriReference\"";
So I am having a ruby app write json response to the console which is read by another perl program that tries to convert the json response back to a perl hash. Here's my problem:
ruby app outputs the correct json output but the console adds a single quote to it like so:
my $ruby_json_out = '"{\"return\":{\"sync_enabled\":false,\"local\":true,\"name\":{\"name\":\"Sam\"}}}"'
my $ret = JSON->new->allow_nonref->decode($ruby_json_out);
Now I expect to get hash_ref in $ret but I get a string: '{"return":{"sync_enabled":false,"local":true,"name":{"name\":"Sam"}}}'.
I have searched all over the net and can't find a solution to this. When I manually stripout the single quote:
"{\"return\":{\"sync_enabled\":false,\"local\":true,\"name\":{\"name\":\"Sam\"}}}",
and run it works.
I am stuck on this for more than a day now and it's driving me crazy. I am new to perl and ruby too so I might be missing something. Any help will be appreciated greatly.
Why do you try to solve the problem on the Perl side? Woudln't it be easier to solve it on the Ruby side?
At any rate, you can use regexps to remove those doble quotes in the same way you do it manually:
my ($good_json) = ($ruby_json_out =~ /^"(.+?)"$/ ;
And then
$good_json=~ s/\\"/"/g;
Which results in
x JSON->new->allow_nonref->decode($good_json)
0 HASH(0xe4b158)
'return' => HASH(0xe4b1b8)
'local' => JSON::XS::Boolean=SCALAR(0xd22f00)
-> 1
'name' => HASH(0xe4afd8)
'name' => 'Sam'
'sync_enabled' => JSON::XS::Boolean=SCALAR(0xd22fd8)
-> 0