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);
Related
This question already has answers here:
How to mask parts of a string with the asterisk character
(5 answers)
Replacing last x amount of numbers
(6 answers)
How to replace string with asterisk(*) using strlen?
(6 answers)
Mask credit card number in PHP
(12 answers)
How can I hide/replace a part of a string in PHP? [duplicate]
(1 answer)
Closed 11 months ago.
I have the below string:
$string = 005390326548;
How can i get this result?
0053****6548
Basically i want to replace starting from the 4 characters the next 4 characters by ****
one-liner solution.
$string= substr_replace("8487013103", "****", 4, 4);
echo $string;
$string = '005390326548';
$retult = substr($string, 0, 4) . '****' . substr($string, 8);
This question already has answers here:
Insert string at specified position
(11 answers)
Closed 3 years ago.
How do I add - in between strings.
Let’s say for instance, I want to add - in 123456789101 at the fourth position three times thereby making it look like this : 1234-5678-9101.
Substr_replace() or str_replace has not solved the problem.
I would use combination of str_split and implode.
$code = 123456789101;
$formatted = implode('-', str_split($code, 4) );
echo $formatted; //1234-5678-9101
There are a number of ways to do this.
Use substr
$output = sprintf('%s-%s-%s', substr($string, 0,4), substr($string,4,4), substr(8,4));
Use preg_replace
$output = preg_replace('/(.{4,4})(.{4,4})(.{4,4})/', '$1-$2-$3', $string);
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);
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);
This question already has answers here:
How to extract a substring from a string in PHP until it reaches a certain character?
(5 answers)
Closed 8 years ago.
I have a long string that will have 8 characters that I need to get out of it. The 8 characters always happen after a specific number.
example:
blahblah1234WMCRCA01therestofthisisntimportant
Out of that I need to set a variable to the 8 characters after 1234 which would be WMCRCA01
This should help:
$string = "blahblah1234WMCRCA01therestofthisisntimportant";
$startingString = "1234";
$startingStringAt = strpos($string, $startingString);
if (false === $startingStringAt) {
echo "Could not find the \"$startingString\"";
} else {
$eightCharacters = substr($string, $startingStringAt + 4, 8);
echo "Eight characters found: " . $eightCharacters;
}
It is a complex solution including the case where no preceding "1234" string occurs in the base string.