Php MySQL SUBSTR()函数
我想比较$images,$uploads,但输出有不同的名称:Php MySQL SUBSTR()函数,php,mysql,Php,Mysql,我想比较$images,$uploads,但输出有不同的名称: $images = '/fiscalapplications/uploads/Image_00314_20200122.pdf'; $uploads = 'Image_00314_20200122.pdf'; 如何为$images to=Image_00314_20200122.pdf执行SUBSTR <?php //pulling images from mysql colum1 require_once 'datab
$images = '/fiscalapplications/uploads/Image_00314_20200122.pdf';
$uploads = 'Image_00314_20200122.pdf';
如何为$images to=Image_00314_20200122.pdf执行SUBSTR
<?php
//pulling images from mysql colum1
require_once 'database_connection.php';
$result = mysqli_query($conn,"SELECT check_pic_path FROM checklog");
$images = []; // store a list of images from the database
while ($row = mysqli_fetch_array($result)) {
$images[] = $row['check_pic_path'] . "<br>";
}
//pulling pictures from specific path
$dir = "fiscalapplications/uploads";
$uploads = []; // store a list of files on disk
if(is_dir($dir)) {
while (($file = readdir($dh)) !== false) {
$uploads[] = $file . "<br>";
}
}
}
//attempting to compare $image to $upload
$compare = array_diff($images, $uploads);
print_r($compare);
使用basename()
从路径名中删除目录:
$images[] = basename($row['check_pic_path']) . "<br>";
$images[]=basename($row['check\u pic\u path'])。“
”;
使用basename()
从路径名中删除目录:
$images[] = basename($row['check_pic_path']) . "<br>";
$images[]=basename($row['check\u pic\u path'])。“
”;
在MySQL中,它可能是
SUBSTRINT_INDEX(tablename.imagefield, '/', -1)
在MySQL中可能是这样
SUBSTRINT_INDEX(tablename.imagefield, '/', -1)
$uploadsnew=substr($images,strrpos($images,“/”)+1);
strrpos获取斜杠最后一次出现的位置;substr返回该位置之后的所有内容
如果没有斜杠,这将无法正常工作,因为strrpos返回false。下面是一个更健壮的版本:
$pos=strrpos($url,“/”);
$uploadsnew=$pos==false$url:substr($images,$pos+1) $uploadsnew=substr($images,strrpos($images,“/”)+1);
strrpos获取斜杠最后一次出现的位置;substr返回该位置之后的所有内容
如果没有斜杠,这将无法正常工作,因为strrpos返回false。下面是一个更健壮的版本:
$pos=strrpos($url,“/”);
$uploadsnew=$pos==false$url:substr($images,$pos+1) 在PHP中:
$images = $images ?: '';
$uploads = substr($images, ($pos = strrpos($images, '/')) ? $pos : 0);
在MySQL中:
if( ifnull( locate( '/', check_pic_path ), 0 ) > 0, substring_index( check_pic_path, '/', -1 ), check_pic_path )
在PHP中:
$images = $images ?: '';
$uploads = substr($images, ($pos = strrpos($images, '/')) ? $pos : 0);
在MySQL中:
if( ifnull( locate( '/', check_pic_path ), 0 ) > 0, substring_index( check_pic_path, '/', -1 ), check_pic_path )
我不知道你在问什么。是否要检查图像文件名是否相同?我建议您不要将“
”放在数组本身中。养成总是把数据和表达分开的习惯。我不知道你在问什么。是否要检查图像文件名是否相同?我建议您不要将“
”放在数组本身中。养成总是将数据与表示分离的习惯。有时简单是最好的!;-)有时候简单是最好的!;-)