0

I was looking and found How to check if variables starts with specific string & How to chech if a variable contains specific string but, I don't get it.

Which one could I use to check if a variable STARTS with a specific string just like a MYSQL LIKE WHERE CLAUSE:

SELECT * FROM table WHERE column LIKE 'string%'
Community
  • 1
  • 1
tachomi
  • 109
  • 6
  • 1
    `preg_match('/^string.*$/', $sString)` ? - though it's probably not worth the RegExp overhead. – CD001 Oct 03 '14 at 15:39

2 Answers2

1

You could use this, to match for any length string. Instead of hardcoding the string and it's length.

$string = "string to test";
$testfor = "strin";
if(substr( $string, 0, strlen($testfor) ) === $teststr) {
      echo "Match";
}
Frederik Spang
  • 3,379
  • 1
  • 25
  • 43
0
if(substr($string, 0, 5) === "String") {
# Do code
} else {
# Error
}

Easy enough for simple strings.

iamgory
  • 862
  • 1
  • 6
  • 10