Grab 8 characters after a particular string [duplicate] - php

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.

Related

How can i replace a portion of string starting from the next 4 characters? [duplicate]

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);

How to insert character in a string at a specific position? [duplicate]

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 "

PHP - If string contains '<' or '>' - strpos() [duplicate]

This question already has answers here:
Detect HTML tags in a string
(8 answers)
Closed 4 years ago.
I want to do a simple kind of test to see if a string contains any HTML.
In this case if the $string variable is test or <test> it returns no for me:
$string = 'test';
if(strpos($string,'<') !== 'false'){
echo 'no';
}else{
echo 'yes';
}
Is there a better way to check if a string contains HTML? I don't want to do anything to the string just check if it has HTML tags?
if($string != strip_tags($string)) {
// contains HTML
}
Took the answer from here

PHP preg_match_all to extract number with or without decimal [duplicate]

This question already has answers here:
Extract floating point numbers from a delimited string in PHP
(6 answers)
Closed 6 years ago.
I am trying to extract any number out from a string (with or without decimal) and dump that into an array. Below is my code
$matches = array();
$str = "I have string of 21.11 out of 30";
preg_match_all("/\d+/",$str,$matches);
echo var_dump($matches);
The current output has 3 elements below:
21
11
30
But I expect to output only 2 elements below:
21.11
30
How do I change my regex?
You should use this regex (add dot in character class) :
$matches = array();
$str = "I have string of 21.11 out of 30";
preg_match_all("/[\d\.]+/",$str,$matches);
var_dump($matches);
Test it (Ctrl + Enter)

How can I find a special character in my string and store all value after this character in a variable [duplicate]

This question already has answers here:
How to get everything after a certain character?
(9 answers)
Closed 8 years ago.
I am trying to find special character $# from my string and trying to store all value after these special in a variable but I could not do this task. How do I find this $# special character in my whole string and store all value after this special charecter in a variable. My string is:
92~SAPL/1200/2012~2~LAXMAN SINGH AND OTHERS~SHANKER SINGH AND OTHERS~D~
2014-04-10~09:31:13
07/04/2014|93~SAPL/275/1998~1~RAM SWAROOP AND OTHERS~BABU RAM AND OTHERS~D~
2014-04-10~09:31:13 07/04/2014|94~FAFOD/52/2013~3~RAM KUMAR PANDEY~RAJENDRA PANDEY AND
ANOTHER~D~2014-04-10~09:31:13 07/04/2014$#2014-04-10~2014-04-09
try this:
$string = "Your Entire String";
$explodedString = explode('$#', $string);
if(count($explodedString) > 1) {
unset($explodedString[0]);
$returnString = count($explodedString) > 1 ? $explodedString : $explodedString[0];
} else {
$returnString = null;
}
if(is_array($returnString)) {
//if you want to divide the string only to the first occurrence of $# leave the below line as is else comment it out
$returnString = implode('', $returnString);
}
echo $returnString;

Categories