删除链接文本mysql的最后一部分



我有一个带有描述列文本的表:

    <center>
texttextffjfjfffkfkf
    <img src="https://example.com/43435565653554/687474703a2f2f736f757" alt="\">
    <img src="https://example.com/22223445556565/687474703a2f243434344" alt="\">
fgfgfgfgfgfgfgfgfgfg
    <center>

我想删除所有链接的最后一部分687474703a2f2f736f757687474703a2f243434344

    <center>
texttextffjfjfffkfkf
    <img src="https://example.com/43435565653554/" alt="\">
    <img src="https://example.com/22223445556565/" alt="\">
fgfgfgfgfgfgfgfgfgfg
    <center>
<?php
$input  = explode('/', 'https://example.com/43435565653554/687474703a2f2f736f757');
$output = implode('/', array_slice($input, 0, 4));
echo $output;

输出:https://example.com/43435565653554

尝试一下。

您可以在mysql

中选择子弦乐
SELECT *, SUBSTRING(columnName, 1, CHAR_LENGTH(columnName)-numOfCharactoerYouWantToRemove) as temp FROM `user`

例如,

SELECT *, SUBSTRING(description, 1, CHAR_LENGTH(description)-2) as temp FROM `user`

在PHP中,您可以接受:

$testText = "https://example.com/43435565653554/687474703a2f2f736f757";
$testText = preg_replace('#(http.+/)[a-zA-Z0-9]+/?$#', '$1', $testText);

在JavaScript中,您可以使用:

var testText = "https://example.com/43435565653554/687474703a2f2f736f757"
var newText = testText.replace(/(http.+/)[a-zA-Z0-9]+/?$/, "$1")

您可以使用正则表达式执行此操作。这就是您可以在php中做到这一点的方式。

$url='https://example.com/43435565653554/687474703a2f2f736f757';
$url= preg_replace('/([A-z]|d)*$/', '', $url);
echo $url;

另一种选择:使用substrstrrpos

<?php
// Obtained from the database, hard coded for the sake of this question
$urls = [
    'https://example.com/43435565653554/687474703a2f2f736f757',
    'https://example.com/22223445556565/687474703a2f243434344'
];
?>
<center>
texttextffjfjfffkfkf
<?php foreach ($urls as $url) { ?>
    <img src="<?= substr($url, 0, strrpos(trim($url, " tnrx0B/"), '/')); ?>" alt="\" />
<?php } ?>
fgfgfgfgfgfgfgfgfgfg
<center>

这是从字符串的开头获取子字符串,然后获得最后发生的/的位置。

由于您说这些URL的格式将始终是相同的,因此您不必担心任何尾声/
话虽这么说,我使用了带有自定义面罩的装饰来删除任何领先或尾随的/

最新更新