This question already has answers here:
Insert string at specified position
(11 answers)
Closed 2 years ago.
$str="Hello From Other Side ";
$add = rand(20,50);
How can I insert a number or character as (25) after every character in a string at a specific position?
result
He25llo Fr25om Ot26her S25ide
Thank you
You can use the substr() function!
If u want to insert 25 inside $str="Hello From Other Side "; at the position 2!
U can do
$pos=2;
$new_string= substr($str, 0, $pos). "25" . substr($str, $pos);
and the results would be:
"He25llo From Other Side "
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:
Get the last word of a string
(10 answers)
How to get the last element of an array without deleting it?
(33 answers)
Closed 1 year ago.
How can I remove all words from a string except the last one with PHP?
For example the string "Organic Black Olives" would become "Olives".
Try this:
$str = "Organic Black Olives";
// get words list
$words = explode(' ', $str);
// get last word
$last_word = $words[count($words)-1];
echo $last_word;
This question already has answers here:
How can I get the last 7 characters of a PHP string?
(8 answers)
Closed 5 years ago.
I have this string in my database like 12-12-2067. I want to only display the last 4 words in the string 2067 in my view. How do i achieve this ?
$get_string = Item::all()->first();
<p>{{get_string}}</p>
You can use:
substr ($getstring->string, -4)
Because of (-) it will start form the end of the string and it will take the next 4 characters.
Take care that first() it will return an object and not the string you want.
Please read the manual substr.
$str = substr($str, -4);
string substr ( string $string , int $start [, int $length ] )
$dynamicstring = "12-07-2017";
$newstring = substr($dynamicstring, -4);
This question already has answers here:
How to remove anything in a string after "-"?
(7 answers)
Closed 7 years ago.
i've a query string like this:
SELECT * FROM `backplanechanneldecoder` WHERE Pair = '3' LIMIT 25,25
and i want to remove all the text which comes after LIMIT.
Note: This string is generated dynamically from another code on which I've no control that's why I've to remove part of string from my end.
Use str_pos to find the word LIMIT and then strip after that using substr
$string = 'SELECT * FROM `backplanechanneldecoder` WHERE Pair = 3 LIMIT 25,25';
$limit_pos = strpos($string, 'LIMIT');
$string = substr($string, 0, $limit_pos);
echo $string;
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.