Allow a-z, A-Z, 0-9, space and -. preg_match - preg-match

How can I allow a-z, A-Z, 0-9, - and space with preg_match.
I currently have the following:
if (!preg_match("/^[_a-zA-Z0-9]+$/", $name))
Also a side-question, does any of you know a good guide to learn preg_match.

I think this will work for you
<?php
$result = preg_match("/^[a-zA-Z0-9 \s]+$/", $name));
?>

just give space and "-" in your brackets [ ]
example
<?php
$name = "A- ";
if(preg_match("/^[_a-zA-Z0-9- ]+$/", $name))
{
echo "hello";
}
?>
and a good site for studying : link

only [a-z, A-Z, 0-9 and Space] allow. We delete the first space with LTRIM.
<?php
$name = "Hasan Yİlmaz SÄ°MFER ";
if(ltrim(preg_match('/^[a-zA-ZığüşöçİGÜŞÖÇ0-9- ]+$/', $name2), ' '))
{
echo $name;
}
?>

Using /^[a-zA-Z'- ]+$/ Shows the below given warning
"preg_match(): Compilation failed: range out of order in character
class at offset 10"
Instead of the above-mentioned code, you can use /^[a-zA-Z' -]+$/. This will not show the warning.!

Related

Using explode but after first <br><br> Pattern

I am using explode to create an array from string and using pattern <br><br> but in my case i want it to be start after first match <br><br>. Means when first time it gets <br><br> then it skip and create an array from second time and so on match <br><br>
<?php
$myString = "Welcome, j.<br><br>
 
(1) this revisional application has been preferred under read with section of the against the order passed by the learned special judge, ndps act, 6th court at barasat.<br><br>
(2) case of the petitioner is that he along with other accused persons are facing trial case, as pending the learned additional sessions judge, 6th court. barasat.";
$myArray = explode('<br><br>', $myString);
// $arr = ltrim($myArray, ' ');
echo "<pre>"; print_r($myArray);
foreach($myArray as $key => $value)
{
$whatIWant = substr($value, strpos($value," "));
echo ucfirst($whatIWant);
}
Basically my task is to Capitalize first letter of Second word i.e this and case.
You can use array_shift() function to shift your array by one element and skip first item in your array.
php document for array_shift
$myArray = explode('<br><br>', $myString);
array_shift($myArray);
foreach($myArray as $key => $value)
{
$whatIWant = substr($value, strpos($value," "));
echo ucfirst($whatIWant);
}
I would try using preg_match_all instead.
The code below looks for the pattern (\d+) (\w+)
\d+ = 1 or more digits
\w+ = 1 or more alphanumeric letters
If you were to add (3)... (4)... and so on, it would match all of them
<?php
$myString = "Welcome, j.<br><br>
(1) this revisional application has been preferred under read with section of the against the order passed by the learned special judge, ndps act, 6th court at barasat.<br><br>
(2) case of the petitioner is that he along with other accused persons are facing trial case, as pending the learned additional sessions judge, 6th court. barasat.";
preg_match_all("/(\(\d+\))\s(\w+)/", $myString, $all_matches);
foreach ($all_matches[1] as $idx => $match) {
$original = "{$match} " . $all_matches[2][$idx];
$replacement = "{$match} " . ucfirst($all_matches[2][$idx]);
$myString = str_replace($original, $replacement, $myString);
}
echo $myString;

how to find substring between given characters in smarty?

I have following code in php to which I am looking in smarty
function get_string_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
$fullstring = '[1]wholesale';
$parsed = get_string_between($fullstring, "[", "]");
$filename = substr(strrchr($fullstring, "]"), 1);
echo $parsed.'/'.$filename;
//Output 1/Wholesale
I want this output 1/Wholesalein two different variables (as done in php $parsed,$filename) in Smarty (Let say string is [1]wholesale).
You could achieve it this way:
{assign var="var" value="[1]wholesale"}
{$var|substr:($var|strpos:'['+1):($var|strpos:']' - $var|strpos:'['-1)}/{$var|substr:($var|strpos:']'+1)}
Output for this will be:
1/wholesale
However as you see it's rather quite illegible. You could of course move it to some function/modifier but as always I need to tell, the best option is to prepare data in PHP and then simply display it in Smarty not using advanced transformations.

preg replace remove the same repeated non-word character

I want to remove the same repeated non-word character.
My code looks like this:
<?php
$name = 'Malines - Blockbuster (prod. Malines) ***** (((((( &%^$';
echo preg_replace('/[^\pL\pN\s]{2,}/u', '', $name);
?>
Malines - Blockbuster (prod. Malines) ***** (((((( &%^$ should be Malines - Blockbuster (prod. Malines) &%^$
Only repeated non-word character should be removed. Could you help me?
You must use back-reference to say that the second character is the same as the one before :
/([^\pL\pN\s])\1+/u
Using the same idea than Arlaud Agbe Pierre (that is the way to do it), the pattern can be shorten using the Xan character class. \p{Xan} is the union of \p{L} and \p{N}. \P{Xan} (with an uppercase "p") is the negation of this class (i.e. all that is not a letter or a number).
$str = 'Malines - Blockbuster (prod. Malines) ààà ***** (((((( &%^$';
echo preg_replace('~(\P{Xan})\1+~u', '$1', $str);
Note: in this pattern, consecutive white characters are removed too.
an other way:
echo preg_replace('~(\P{Xan})\K\1+~u', '', $str);
where \K resets the begining of the match from the match result. (note that you can however define capturing groups before that you can use after)

Simple regular expression using preg_match

I am trying to retrieve the three digit number after this word and semi colon.
REF: 222
The code i have below works but its not good because its getting 3digit numbers from the $decoded_message string.
What i really want is only to grab three digit number after the word REF: ###
if (preg_match("([0-9]{3})", $decoded_message, $matches)) {
echo "Match was found <br />";
echo "ref = ".$matches[0];
}
Thanks in advance
You can search for REF: [0-9]{3} and then remove the REF: part.
if (preg_match("/REF: [0-9]{3}/", $decoded_message, $matches)) {
echo "Match was found <br />";
echo "ref = ".substr( $matches[0], 5 );
}
You may simply replace "REF: " by using
$output = preg_replace("/REF: /","", "REF: 222");
Afterwards, only the number should be contained in $output.

What is the Ruby equivalent of preg_quote()?

In PHP you need to use preg_quote() to escape all the characters in a string that have a particular meaning in a regular expression, to allow (for example) preg_match() to search for those special characters.
What is the equivalent in Ruby of the following code?
// The content of this variable is obtained from user input, in example.
$search = "$var = 100";
if (preg_match('/' . preg_quote($search, '/') . ";/i")) {
// …
}
You want Regexp.escape.
str = "[...]"
re = /#{Regexp.escape(str)}/
"la[...]la[...]la".gsub(re,"") #=> "lalala"

Resources