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

时间:2018-04-27 09:47:04

标签: php 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>

5 个答案:

答案 0 :(得分:2)

<?php

$input  = explode('/', 'https://example.com/43435565653554/687474703a2f2f736f757');
$output = implode('/', array_slice($input, 0, 4));
echo $output;

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

试试这个。

答案 1 :(得分:0)

您可以在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`

答案 2 :(得分:0)

在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")

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

另一种选择:使用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, " \t\n\r\0\x0B/"), '/')); ?>" alt="\\" />
<?php } ?>
fgfgfgfgfgfgfgfgfgfg
<center>

这样做是从字符串的开头获取子字符串,然后获取最后一个/的位置。

由于您声明这些网址的格式始终相同,因此您无需担心任何尾随/
话虽如此,我使用带有自定义蒙版的修剪来移除任何前导或尾随/