I'm converting one of my old Perl programs to PHP, but having trouble with PHP's string handling in comparison to Perl.
In Perl, if I wanted to find out if $string contained this, that or the_other I could use:
if ($string =~ /this|that|the_other/){do something here}
Is there an equivalent in PHP?
You can either use a regular expression (e.g. preg_match):
if(preg_match('/this|that|the_other/', $string))
or make it explicit (e.g. strstr):
if(strstr($string, 'this') || strstr($string, 'that') || strstr($string, 'the_other'))
You can use PHP's preg_match function for a simple regex test.
if ( preg_match( "/this|that|the_other/", $string ) ) {
...
}
In PHP you can use preg_match function as:
if( preg_match('/this|that|the_other/',$string) ) {
// do something here.
}
Related
I have some words with | between each one and I have tried to use preg_match to detect if it's containing target word or not.
I have used this:
<?php
$c_words = 'od|lom|pod|dyk';
$my_word = 'od'; // only od not pod or other word
if (preg_match('/$my_word/', $c_words))
{
echo 'ok';
}
?>
But it doesn't work correctly.
Please help.
No need for regular expressions. The functions explode($delimiter, $str); and in_array($needle, $haystack); will do everything for you.
// splits words into an array
$array = explode('|', $c_words);
// check if "$my_word" exists in the array.
if(in_array($my_word, $array)) {
// YEP
} else {
// NOPE
}
Apart from that, your regular expression would match other words containing the same sequence too.
preg_match('/my/', 'myword|anotherword'); // true
preg_match('/another/', 'myword|anotherword'); // true
That's exactly why you shouldn't use regular expressions in this case.
You can't pass a variable into a string with single quotes, you need to use either
preg_match("/$my_word/", $c_words);
Or – and I find that cleaner :
preg_match('/' .$my_word. '/', $c_words);
But for something as simple as that I don't even know if I'd use a Regex, a simple if (strpos($c_words, $my_word) !== 0) should be enough.
You are using preg_match() the wrong way. Since you're using | as a delimiter you can try this:
if (preg_match('/'.$all_words.'/', $my_word, $c_words))
{
echo 'ok';
}
Read the documentation for preg_match().
Is there a simple way to use ltrim() to remove a single instance of a match instead of all matches?
I'm looping through array of strings and I'd like to remove the first, and only first, match (vowels in this case):
ltrim($value, "aeiouyAEIOUY");
With default behavior the string aardvark or Aardvark would be trimmed to be "rdvark". I'd like result to be "ardvark".
I'm not bound to ltrim by any means but it seemed the closest built-in PHP function. It would be nice of ltrim and rtrim had an optional parameter "limit", just saying... :)
Just use preg replace it has a limit option
eg
$value = preg_replace('/^[aeiouy]/i', '', $value, 1);
Regular expressions is probably overkill, but:
$value = preg_replace('/^[aeiouy]/i', '', $value);
Note the i makes it case-insensitive.
You can't use ltrim to do this for the reasons you say, nor can you use str_replace (which also has no limit). I think it's easiest just to use a regex:
$value = preg_replace('/^[aeiouy]/i', '', $value);
However if you really don't want to do that, you can use a substring, but you would have to check the position of any of those strings in the string in a loop as there is no php function that does such a check that I know of.
You can use the preg_replace function:
<?php
$value = preg_replace('/^[aeiouy]/i', '', $value);
?>
There are several way you can go about doing what you are looking to do.
Perhaps most straightforward would be a regular expression replacement like this:
$pattern = '/^[aeiouy]{1}/i';
$result = preg_replace($pattern, '', $original_string);
This is probably the most efficient way (so ignore my regular expressions answer):
if (strpos('aeiouyAEIOUY', $value[0]) !== false) $value = substr($value, 1);
Or,
if (stripos('aeiouy', $value[0]) !== false) $value = substr($value, 1);
is it possible to use wildcard in an if statement?
My code:
*=wildcard
if ($order_info=='Quatro*') {
}
$order_info will be "Quatro - na splátky", or "Quatro - čťžýáí"
Use regex:
if (preg_match('/^Quatro/', $order_info)) {
}
or strpos:
if (strpos($order_info, 'Quatro') === 0) {
}
Edit: Avoiding regex engine invocation for simple string matches like this is usually preferred. strpos will do the same job less expensively.
Sure, use a regex:
if( preg_match( '/^Quatro.*/', $order_info))
{
}
No. Use preg_match or strpos instead.
Regular expressions will do this.
Or you could do:
if($order_info.substr(0, 6) == 'Quatro')
An example would be "SU1203" or "UP1234" or any two letters followed by numeric values.
thanks
This can be solved with the quite simple expression
^[A-Z]{2}\d+$
In JavaScript you can use the test() method:
if(/^[A-Z]{2}\d+$/.test(str))
and in PHP, preg_match:
if(preg_match('/^[A-Z]{2}\d+$/', $str) === 1)
I suggest to learn regular expressions.
See also:
Regular expressions in JavaScript
Regular expressions in PHP
Try:
if (preg_match('|^[A-Z]{2}[0-9]+$|', $string_to_test))
{
// matched
}
javascript example:
if ( /(^[A-Z]{2}\d+)/.test('SU1203') ) {
alert( 'matched' )
} else { alert('not matched') }
I'm looking to make a statement in PHP like this:
if(preg_match("/apples/", $ref1, $matches)) OR if(preg_match("/oranges/", $ref1, $matches)) {
Then do something }
Each of those above by themselves work just fine, but I can't figure out how to make it so that if either of them is true, then to perform the function I have beneath it.
Use the | to select one value or the other. You can use it multiple times.
preg_match("/(apples|oranges|bananas)/", $ref1, $matches)
EDIT: This post made me hungry.
To group your patterns and capture the result in $matches:
preg_match('/(apples|oranges)/', $ref1, $matches)
To group your patterns without capturing the result in $matches (most relevant if you're doing other parenthesis capturing and don't want this to interfere):
preg_match('/(?:apples|oranges)/', $ref1, $matches)
Simple, use the logical OR operator:
if (expression || expression) { code }
For example, in your case:
if(
preg_match("/qualifier 1/", $ref1, $matches) ||
preg_match("/qualifier 2/", $ref1, $matches)
) {
do_something();
}