$vari = "testing 245";
$numb = 0..9;
$numb_pos = strpos($vari,$numb);
echo substr($vari,0,$numb_pos);
The $numb is numbers from 0 to 9
Where am I wrong here, all I need to echo is testing
You want to cut out the numbers from a string?
$string = preg_replace('/(\d+)/', '', 'String with 1234 numbers');
Use a regular expression to strip numeric characters from your string.
or, use a regular expression to find the first instance of one either way...
Your code won't work as-is, as it'll fail if the number if the first character in the string. (You need to check $numb_pos !== false prior to the substr.)
Irrespective, if you just want to check for the existance of a number in a string, something like the following would probably be more efficient.
$digitMatched = preg_match('/\\d/im', $vari);
Related
Can we use Bitwise operator "|" with strpos in php?
I need to check if a0,a1,a2,a5 strings are exists in the given $status variable.
My code is given bellow.My code will return values(position) only when the status variable have value=a0 or a1 or a2 or a5.It will return false when $status='a1 test string.
$status='a1 test string';
echo strpos("|a0|a1|a2|a5|", $status);
No, you cannot. Documentation does not mention anything remotely similar:
strpos — Find the position of the first occurrence of a substring in
a string
Find the numeric position of the first occurrence of needle in the
haystack string.
Parameters
haystack The string to search in.
needle If needle is not a string, it is converted to an integer and
applied as the ordinal value of a character.
offset If specified, search will start this number of characters
counted from the beginning of the string. If the offset is negative,
the search will start this number of characters counted from the end
of the string.
In fact, it wouldn't make much sense to implement such feature since you already have a full-fledged regular expression engine:
$has_substrings = (bool)preg_match('/a0|a1|a2|a5/u', $status);
You can use it like this. Here | means or
<?php
$status='a1 test string';
if(preg_match("/\b(a0|a1|a2|a5)\b/", $status))
{
echo "Matched";
}
Can we use Bitwise operator "|" with strpos in php?
as a Bitwise operator | - No
as a literal symbol | - Yes
You cannot do that with a single string search. You need to use either a regular expression which can test for multiple options at once, or you need to iterate over your search terms.
Sahil Gulati gave a simple example for a regular expression based approach.
Here is a simple iteration based approach:
<?php
$status = 'a1 test string';
$search = explode('|', substr("|a0|a1|a2|a5|", 1, -1));
// would be much easier to start with an array of search tokens right away:
// $search = ['a0', 'a1', 'a2', 'a5'];
$result = false;
array_walk($search, function($token) use ($status, &$result) {
$result = (FALSE!==strpos($status, $token)) ? true : $result;
});
var_dump($result);
Using a for loop, I want to cycle through each character in a string and check to see if it is a certain letter. Let's say I want to search my string for my favorite letters -- A,C,D,O,V. Let's say I have a string, $giantButtText. Why does this result in no output on my standard output (given that $giantButtText does indeed contain those letters)?
if($giantButtText[$i] == "/[acdov]/") echo $giantButtText[$i];
Cheers!
You are trying to match $giantButtText[$i] to a regular expression.
The standard way to do this is preg_match() (http://php.net/manual/en/function.preg-match.php).
Something like this should work:
$a = array();
$a[0] = "dadov";
if (preg_match("/[acdov]/", $a[0])) echo "true";
-> true
I have a string like any of the following:
$str = 'A001';
$str = 'B001';
$str = 'AB001';
$str = 'B0015';
....
$str = '001A';
I want to keep only 3 characters from the end of each string.
My code is like this:
$code = str_split($str);
$code = $code[1].$code[2].$code[3];
But it works for specific cases, but not for general ones! How I can get it for general ones?
I want to keep every 3 character from end of string
Simply Use substr
echo substr($str,-3); // Last 3 characters
Second parameter to this function is start, and according to the Manual
If start is negative, the returned string will start at the start'th character from the end of string.
Fiddle
Use sbstr()
echo substr($str,-3);//get last 3 char char
Or try:
echo $str[strlen($str)-3].$str[strlen($str)-2].$str[strlen($str)-1];
You need to use substr function.
All you need to do is to pass the string, and tell it where you cut the string off. If you want to cut the string off from end, you have to provide the value in negative.
substr($str, -3);
// The third argument is optional, which specifies the length of the returned string.
When I try to match some user input code I always get 0 as returned value.
$input = $_POST['input'];
$look = '[a-zA-Z]';
preg_match($look,$input);
For some reason I always get 0 as return value, why?
Couple of issues here in your regex:
Regex needs a delimiter so it should be `/[a-zA-Z]/'
You are probably more than single US English letter so better to use + modifier to match more than 1 letter
Not using start and end of line anchors can cause problems and you may get false positive from your preg_match call.
Combining all suggestions, you can use this regex:
$look = '/^[a-zA-Z]+$/';
OR
$look = '/^[a-z]+$/i';
<?php
$input = $_POST['input'];
$look = '/^[a-zA-Z]/';
preg_match($look,$input);
?>
See Manual
I want to exclude a specific number like 4800 from a string of numbers like 569048004801.
I'm using php for this and the method preg_match_all some pattern's examples I have tried :
/([^4])([^8])([^0])([^0])/i
/([^4800])/i
If you just want to see if a string contain 4800, you don't need regular expressions :
<?php
$string = '569048004801';
if(strpos($string,'4800') === false){
echo '4800 was not found in the string';
}
else{
echo '4800 was found in the string';
}
More information about strpos in the documentation here
If you mean you simply want to remove 4800 from a string, this is easier with a str_replace:
$str = '569048004801';
$str = str_replace('4800', '', $str);
On the other hand, if you mean you want to know if a particular string of digits contains 4800, this will test that for you:
$str = '569048004801';
if (preg_match_all('/4800/', $str) > 0) {
echo 'String contains 4800.';
} else {
echo 'String does not contain 4800.';
}
/([^4])([^8])([^0])([^0])/i
This actually says, a sequence of four characters that is not "4800". Close.
/([^4800])/i
This actually says, a single character that is not '4', '8', or '0'.
Assuming you mean to capture a number that doesn't contain "4800" in it, I think you might want
/(?!\d*4800)\d+/i
This says, check first that we're not looking at a string of numbers with "4800" somewhere, and provided this is the case, capture the string of numbers. It's called a "negative lookahead assertion".