开发者

if statement variable starts with

开发者 https://www.devze.com 2023-02-24 02:13 出处:网络
Is there a way of asking if a variable starts with... The problem i have is in a database i have both image 开发者_StackOverflow中文版files and audio files i would like an if statement that said if $

Is there a way of asking if a variable starts with...

The problem i have is in a database i have both image 开发者_StackOverflow中文版files and audio files i would like an if statement that said if $file starts with "audio/......."


if (strpos($file,'audio/') === 0 ) 
{
  echo "audio/ found at start";
}


You can use substr() to get a part of a string, and then you can check if it is equal to 'audio/'.

if (substr($file, 0, 6)=='audio/') {
}


There is no such builtin function, but you can use string comparison for equivalent behaviour

if (strncmp('audio/', $file, 6) === 0) {
  // do something
}

Or a more common, but less performant solution

if (substr($file, 0, 6) === 'audio/') {
  // do something
}

However, you should be able to select the corresponding rows right from the database

WHERE column LIKE 'audio/%'


why not use substr like

if (substr($file,0,5) == "audio")

etc


if ( substr($file,0,strlen($start))===$start ) ...

or

if ( substr_compare($file,$start,0,strlen($start),true)==0 ) ...
0

精彩评论

暂无评论...
验证码 换一张
取 消