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".
Related
I have a question about a String i want to count all the characters in the string. Like if i have a string
"Hello world & good morning. The date is 18.05.2016"
You can use explode() to convert string into array and then use count() function to count length of array.
echo count(explode(' ', "Hello world & good morning. The date is 18.05.2016"))
You can try this code.
<?php
$file = "C:\Users\singh\Desktop\Talkative Challenge\base_example.txt";
$document = file_get_contents($file);
$return_array = preg_split("/[\s,]+/",$document);
echo count($return_array);
echo $document;
?>
Hopefully it will be working fine.
The 3rd parameter of str_word_count allows you to set additional characters to be counted as words:
str_word_count($document, 0, '&.0..9');
&.0..9 means it will consider &, ., and range from 0 to 9.
You can count the spaces with substr_count and add one.
echo substr_count($str, " ")+1;
// 9
https://3v4l.org/oJJkt
I have a string as : ABSOLUTEWORKLEADSTOSUCCESS
I have another string as : '+'
Now how can I insert the second string at various indexes lets say (3,6,9) of the first string.
PS: I know how to do it via substr(). What i am looking for is something using regex/preg_replace()
Disclaimer: I think that the solution below is stupid, but it does exactly what you ask for: inserts a plus sign at specific indexes using a regular expression and preg_replace function:
<?php
// find 3 groups: three first symbols, two after them, and two more
// find the pattern from the beginning of a string
$regex = '/^(.{3})(.{2})(.{2})/';
$str = 'ABSOLUTEWORKLEADSTOSUCCESS';
// perform a replace: use first group (3 symbols), insert a plus
// then use a second group (2 symbols) and insert another plus,
// then use a third group (2 more symbols) and insert the last plus
$out = preg_replace($regex, '$1+$2+$3+', $str);
echo $out;
Preview here.
You can't insert string with preg_replace, for that you need to loop through the indexes and insert second string at specific index by using substr_rplace as follow
$var = 'ABSOLUTEWORKLEADSTOSUCCESS';
$indexes = array(3,6,9);
$newString = $var;
foreach($indexes as $key=>$value) {
$newString = substr_replace($newString, '+', $value+$key, 0) . "\n";
}
echo $newString;
check output here : https://eval.in/714177
Morning SO. I'm trying to determine whether or not a string contains a list of specific characters.
I know i should be using preg_match for this, but my regex knowledge is woeful and i have been unable to glean any information from other posts around this site. Since most of them just want to limit strings to a-z, A-Z and 0-9. But i do want some special characters to be allowed, for example: ! # £ and others not in the below string.
Characters to be matched on: # $ % ^ & * ( ) + = - [ ] \ ' ; , . / { } | \ " : < > ? ~
private function containsIllegalChars($string)
{
return preg_match([REGEX_STRING_HERE], $string);
}
I originally wrote the matching in Javascript, which just looped through each letter in the string and then looped through every character in another string until it found a match. Looking back, i can't believe i even attempted to use such an archaic method. With the advent of json (and a rewrite of the application!), i'm switching the match to php, to return an error message via json.
I was hoping a regex guru could assist with converting the above string to a regex string, but any feedback would be appreciated!
Regexp for a "list of disallowed character" is not mandatory.
You may have a look at strpbrk. It should do the job you need.
Here's an example of usage
$tests = array(
"Hello I should be allowed",
"Aw! I'm not allowed",
"Geez [another] one",
"=)",
"<WH4T4NXSS474K>"
);
$illegal = "#$%^&*()+=-[]';,./{}|:<>?~";
foreach ($tests as $test) {
echo $test;
echo ' => ';
echo (false === strpbrk($test, $illegal)) ? 'Allowed' : "Disallowed";
echo PHP_EOL;
}
http://codepad.org/yaJJsOpT
return preg_match('/[#$%^&*()+=\-\[\]\';,.\/{}|":<>?~\\\\]/', $string);
$pattern = preg_quote('#$%^&*()+=-[]\';,./{}|\":<>?~', '#');
var_dump(preg_match("#[{$pattern}]#", 'hello world')); // false
var_dump(preg_match("#[{$pattern}]#", 'he||o wor|d')); // true
var_dump(preg_match("#[{$pattern}]#", '$uper duper')); // true
Likely, you can cache the $pattern, depending on your implementation.
(Though looking outside of regular expressions, you're best of with strpbrk as mentioned here too)
I think what you're looking for can be greatly simplified by including the characters that you want to allow like so:
preg_match('/[^\w!#£]/', $string)
Here's a quick breakdown of what's happening:
[^] = not included
\w = letters and numbers
! # £ = the list of characters you would also like to allow
I would like to know how I could find out in PHP if a variable only contains 1 word. It should be able to recognise: "foo" "1326" ";394aa", etc.
It would be something like this:
$txt = "oneword";
if($txt == 1 word){ do.this; }else{ do.that; }
Thanks.
I'm assuming a word is defined as any string delimited by one space symbol
$txt = "multiple words";
if(strpos(trim($txt), ' ') !== false)
{
// multiple words
}
else
{
// one word
}
What defines one word? Are spaces allowed (perhaps for names)? Are hyphens allowed? Punctuation? Your question is not very clearly defined.
Going under the assumption that you just want to determine whether or not your value contains spaces, try using regular expressions:
http://php.net/manual/en/function.preg-match.php
<?php
$txt = "oneword";
if (preg_match("/ /", $txt)) {
echo "Multiple words.";
} else {
echo "One word.";
}
?>
Edit
The benefit to using regular expressions is that if you can become proficient in using them, they will solve a lot of your problems and make changing requirements in the future a lot easier. I would strongly recommend using regular expressions over a simple check for the position of a space, both for the complexity of the problem today (as again, perhaps spaces aren't the only way to delimit words in your requirements), as well as for the flexibility of changing requirements in the future.
Utilize the strpos function included within PHP.
Returns the position as an integer. If needle is not found, strpos()
will return boolean FALSE.
Besides strpos, an alternative would be explode and count:
$txt = trim("oneword secondword");
$words = explode( " ", $txt); // $words[0] = "oneword", $words[1] = "secondword"
if (count($words) == 1)
do this for one word
else
do that for more than one word assuming at least one word is inputted
$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);