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

Php 删除链接文本mysql的最后一部分,php,mysql,Php,Mysql,我有一个带有描述列文本的表: <center> texttextffjfjfffkfkf <img src="https://example.com/43435565653554/687474703a2f2f736f757" alt="\\"> <img src="https://example.com/22223445556565/687474703a2f243434344" alt="\\"> fgfgfgfgfgfgfgfgfgf

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

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

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

另一种选择:使用
substr
strrpos

<?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>

text文本FFJFJFFFKF
“alt=“\\”/>
FGFGFGFGFGFGFGFG
它的作用是从字符串的开头获取一个子字符串,然后获取最后出现的
/
的位置

由于您声明这些URL的格式将始终相同,因此您不必担心任何后续的
/


也就是说,我使用了一个带有自定义掩码的trim来删除任何前导或尾随的
/

所有链接都将遵循该格式吗?如果是,那么您可以设置一个正则表达式来捕获和替换这些img标记是硬编码的还是由PHPjustCarty生成的?是的,在重新阅读我的评论和问题后,所有链接都将遵循该格式;很清楚,I d我不完全理解这个问题。正则表达式方法不是一个好方法…也许
substr
是一个更好的方法不使用此代码发布您正在执行的查询
$url='https://example.com/43435565653554/687474703a2f2f736f757';
$url= preg_replace('/([A-z]|\d)*$/', '', $url);
echo $url;
<?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>