Replace Character From String in PHP [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
I have string in PHP like below
C:\xampp\htdocs
I want output of it using str_replace like
/xampp/htdocs
I am trying it like below
$path = getcwd();
$new = str_replace(array("C:", "\"), ("","/") $path);
echo $new;
but its giving me error like below
Parse error: syntax error, unexpected '","' (T_CONSTANT_ENCAPSED_STRING), expect
ing ')' in C:\xampp\htdocs\install-new.php on line 16
Let me know what is wrong with it.

It's because you escaped a double quote.
You need to add an backslash to your second expression like that:
$path = getcwd();
$new = str_replace(array("C:", "\\"), ("","/") $path);
echo $new;

You are missing array declaration on the 2nd argument, as well as a comma before the 3rd argument $path. Lastly as noted in the comments, the \ needs to be escaped otherwise it escapes the closing quote:
This:
$new = str_replace(array("C:", "\"), ("","/") $path);
Should be:
$new = str_replace(array('C:', '\\'), array('','/'), $path);

Related

Adding two Strings in php leads to unexpected results [duplicate]

This question already has answers here:
JSON: why are forward slashes escaped?
(5 answers)
Closed 2 years ago.
I am trying to add 2 Strings like this:
$response = array();
$target_dir = "uploads/";
$public_key = "f1vlje6378uh6ucok8sda1exo5lmbu";
$target_dir .= $public_key."/";
$response["target_dir created"] = $target_dir;
echo json_encode($response);
"target_dir created": "uploads\/f1vlje6378uh6ucok8sda1exo5lmbu\/"
Any ideas why this "\/" appear and not just "/" ?
It's the default behavior where it escapes the slash.
If you do not want to get the slashes to be escaped, you can do it like this:
echo json_encode($response, JSON_UNESCAPED_SLASHES);
Here's the documentation of json_encode() and the various options you can use: https://www.php.net/manual/en/function.json-encode.php

how to fix parse error syntax error, unexpected '"', expecting ',' or ')' [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 3 years ago.
Parse error: syntax error, unexpected '"', expecting ',' or ')' in /home/givecoin/public_html/script/common.php on line 475
$q = $sql->prepare("UPDATE `".$dbtable_prefix."Settings` SET `value` = ? WHERE `name` = 'version'");
line475
Try by adding new line after each string part. Eg:
$q = $sql->prepare(
"UPDATE `" .
$dbtable_prefix .
"Settings` SET `value` = ? WHERE `name` = 'version'"
);
This won't fix the problem, but it will provide you a better line information: the code you posted does not have any parsing error, so I think your problem is before the line 475.
When PHP, or Byson/FLEX (the parser/lexer behind php), tells you that there was an unexpected character, then it tells you what it expect: the line simply correspond to where it could not find the expected characters.
If it awaits a ',' or a ')', this means you probably have an error before the line 475, for example:
474. $a = ($b
//------^ missing ')'
475. $q = $sql->prepare("")
This would (I did not test) fail

Get First 2 Characters In a String [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I'm trying to get first 2 characters in a string. Here's my code:
$s = 'hello'; // line 4
printf("[%0.2s]\n", %s); // line 5: Gets first 2 characters [he]
It's giving me an error:
Parse error: syntax error, unexpected '%' in
C:\laragon\www\karakter_tamamlama.php on line 5
Why am I getting error?
Use substring function to get letters from string.
An example:
substr($s, 0, 2)
$s instead of %s
printf("[%0.2s]\n", $s);
or
substr($s, 0,2);
Second arg should be $s, see printf()
printf("[%0.2s]\n", $s);

How to set a PHP variable inside an `str_replace`? [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 7 years ago.
What is the correct form to make $id behave as a PHP variable inside the str_replace command? I've tried wrapping the $id inside with {, or ., but nothing helped. I'm not even sure how to define the problem I'm having here so I didn't really know how to Google this:
$id="Something";
$new = str_replace('?abc', '?id=$id&abc', $original);
There are two ways to concatenate strings in PHP. In your example, it would be either:
$new = str_replace('?abc', '?id=' . $id . '&abc', $original);
or
$new = str_replace('?abc', "?id=$id&abc", $original);
Note that the first option is slightly more efficient, and the spaces are optional.
i think it needs to be work like this.
$search = 's';
$replace = 'r';
$subject = 'subject';
$result = str_replace($search,$replace,$subject);
var_dump($result);
and the result will be 'rubject'.

PHP ltrim() not working as expected [duplicate]

This question already has answers here:
PHP ltrim behavior with character list
(2 answers)
Closed 8 years ago.
I have this code..
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = ltrim($homepage1, 'datastring=/mac_project');
echo $trimmed;
I get the output as folio/kitchen/images/03.jpg. It's missing the /port from the /portfolio directory.
Full output should've been /portfolio/kitchen/images/03.jpg
Why not do the simple str_replace() ?
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = str_replace('datastring=/mac_project','',$homepage1);
echo $trimmed;// "prints" /portfolio/kitchen/images/03.jpg
The second parameter for ltrim is for character_mask, which means all the chars in the list will be trimmed.
You could use str_replace(), or if you want to replace only at the beginning of the string by preg_replace():
$trimmed = preg_replace('~^datastring=/mac_project~', '', $homepage1);

Categories