检查数组php中的相似文本 [英] Check for similar text in arrays php

查看:97
本文介绍了检查数组php中的相似文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,该代码显示带有视频文件链接的徽标。我经常上传文件,并且想保持秩序。目前,它只是按名称顺序附加它们。因此,例如,如果我上传电影而又没有上传名称完全相同的照片,则从那时起,所有照片上的链接都将显示为不匹配。

I have this code which displays logos with a link to a video file. I frequently upload files and I want to keep thinks in order. At the moment it just attaches them in order of name. So for example if I upload a movie and haven't gotten around to uploading a photo with the exact same name then from that point on any photos will be displayed with mismatching links.

我想做的是如果名称不匹配,则显示一张临时照片。

What I'd like to do is display a temporary photo if the name doesn't match.

我知道您可以执行类似if(array [1] === array2 [1]),但文件扩展名会有所不同,因此每次都会返回false。

I know you can do something like if(array[1] === array2[1]) but the file extensions would be different so that would return false every time.

代码:

<?php
$images = glob('./*.{jpeg,jpg,png}', GLOB_BRACE);
$movies = glob('./*.{mp4,m4v}', GLOB_BRACE);
$movieLink = 0;

foreach($images as $image) {
    echo '<a href="' .$movies[$movieLink].'">
            <img src="'.$image.'" style="width:300px;height:350px;border:0;">
        </a>';
    $movieLink++;
}

?>

服务器目录示例(400多个电影和30张以上的照片):

Example of server directory (400+ movies and >30 photos):

Dir1


  • Movie1.mp4↰__正确对

  • Movie1 .png ↲

  • Movie2.m4v↰__正确对

  • Movie2.png ↲

  • Movie3.mp4↰__错误配对

  • Movie4.mp4↲

  • Movie4.png←-没有与之配对的图像

  • Movie1.mp4 ↰__ correct pair
  • Movie1.png  ↲
  • Movie2.m4v ↰__ correct pair
  • Movie2.png  ↲
  • Movie3.mp4 ↰__ incorrect pair
  • Movie4.mp4 ↲
  • Movie4.png ←-- no image to pair with

此命令运行时会并排显示3张照片,当您单击前2张照片(Movie1.png&& Movie2.png)时,您会被带到每部正确的电影。但是,当您单击 Movie 4.png时,将被带到 Movie3.mp4。

When this runs it displays 3 photos side by side which when clicked the first 2 (Movie1.png && Movie2.png) you are taken to the correct movies for each. However, when you click "Movie 4.png" you are taken to "Movie3.mp4".

推荐答案

使用 pathinfo($ filename,PATHINFO_FILENAME)构造一个查找数组,以方便验证。查找数组使用无扩展名电影文件名作为键,并与图像文件名(具有扩展名)配对。

Use pathinfo($filename, PATHINFO_FILENAME) to construct a lookup array for easy verification. The lookup array uses "extensionless" movie filenames as keys which are paired with the image filename (with its extension).

您应该循环使用 $ movies 数组,如果您的项目逻辑表明电影总比图像多。

You should be looping the $movies array, if your project logic states that there will always be more movies than images.

$movies = glob('./*.{mp4,m4v}', GLOB_BRACE);
$images = glob('./*.{jpeg,jpg,png}', GLOB_BRACE);
foreach ($images as $image) {
    $lookup[pathinfo($image, PATHINFO_FILENAME)] = $image;
}

foreach ($movies as $movie) {
    $image = $lookup[pathinfo($movie, PATHINFO_FILENAME)] ?? 'default.jpg';
    echo '<a href="' . $movie . '">
            <img src="' . $image . '" style="width:300px;height:350px;border:0;">
        </a>';
}

以上代码段未经测试,但应该非常接近。如果您不熟悉, ?? 是空合并运算符。基本上是说分配查找值,除非它丢失,在这种情况下,请使用默认值。

The above snippet is not tested, but it should be pretty close. In case you are unfamiliar, the ?? is the null coalescing operator. It basically says assign the lookup value unless it is missing, in which case use the default value.

这是查找数组构造的演示: https://3v4l.org/HJhVR

Here's a demo of the lookup array construction: https://3v4l.org/HJhVR

这篇关于检查数组php中的相似文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆