Asked  9 Months ago    Answers:  5   Viewed   59 times

What's the best way to achieve the following:

I have a $img variable containing e.g. myimage_left.jgp, someimage_center.jpg or img_right.jpg

What's the best way to test for _left, _right or _center of the filename and extract this value and store it in a variable?

So I have $img which already contains the complete basename of the file. And I need to have $pos which should hold _center or _left or _right.

What's the way to do that? preg_match, strpos, etc?

 Answers

46

A regex would be simplest:

$input = 'foo_left.jpg';
if(!preg_match('/_(left|right|center)/', $input, $matches)) {
    // no match
}

$pos = $matches[0]; // "_left", "_right" or "_center"

See it in action.

Update:

For a more defensive-minded approach (if there might be multiple instances of "_left" and friends in the filename), you can consider adding to the regex.

This will match only if the l/r/c is followed by a dot:

preg_match('/(_(left|right|center))./', $input, $matches);

This will match only if the l/r/c is followed by the last dot in the filename (which practically means that the base name ends with the l/r/c specification):

preg_match('/(_(left|right|center))\.[^\.]*$/', $input, $matches);

And so on.

If using these regexes, you will find the result in $matches[1] instead of $matches[0].

Wednesday, March 31, 2021
 
braindamage
answered 9 Months ago
33

Not tested, but probably something like if(preg_match("/^[0-9,]+$/", $a)) $a = str_replace(...)

Wednesday, March 31, 2021
 
axiomer
answered 9 Months ago
78

To extend your current code you could use an array of target words to search for, and use a loop:

$a = 'How are you ?';

$targets = array('How', 'are');

foreach($targets as $t)
{
    if (strpos($a,$t) !== false) {
        echo 'one of the targets was found';
        break;
    }
}

Keep in mind that the use of strpos() in this way means that partial word matches can be found. For example if the target was ample in the string here is an example then a match will be found even though by definition the word ample isn't present.

For a whole word match, there is an example in the preg_match() documentation that can be expanded by adding a loop for multiple targets:

foreach($targets as $t)
{
    if (preg_match("/b" . $t . "b/i", $a)) {
        echo "A match was found.";
    } else {
        echo "A match was not found.";
    }
}
Wednesday, March 31, 2021
 
CBroe
answered 9 Months ago
11

Perhaps you didn't know that you can put multiple conditions on a single case:

case mystr
  when "abc", "def", "ghi", "xyz"
    ..
end

But for this specific string-based test, I would use regex:

if mystr =~ /A(?:abc|def|ghi|xyz)z/

If you don't want to construct a regex, and you don't want a case statement, you can create an array of objects and use Array#include? test to see if the object is in the array:

if [a,b,c,d].include?( o )

or, by monkey-patching Object, you can even turn it around:

class Object
  def in?( *values )
    values.include?( self )
  end
end

if o.in?( a, b, c, d )
Monday, July 5, 2021
 
nomie
answered 5 Months ago
61

This can't work properly. Stored with Unicode there are many more Characters than with ANSI. So if you "convert" to ANSI, you will loose lots of charackters.

http://php.net/manual/en/function.htmlentities.php

You can use Unicode (UTF-8) charset with htmlentities:

string htmlentities ( string $string [, int $flags = ENT_COMPAT [, string $charset [, bool $double_encode = true ]]] )

htmlentities($myString, ENT_COMPAT, "UTF-8"); should work.

Thursday, August 5, 2021
 
CoderGuy123
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share