第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使 php 正則表達(dá)式 preg_replace 工作

如何使 php 正則表達(dá)式 preg_replace 工作

PHP
繁華開滿天機(jī) 2023-06-24 17:55:28
我必須使用 轉(zhuǎn)換字符串preg_replace。下面是我需要轉(zhuǎn)換的字符串@[ Test Career 12](career:235)@[ Testing11](business:2)@[ Username](user:1)some text我已經(jīng)創(chuàng)建了代碼來替換內(nèi)容,但它不起作用。請(qǐng)檢查下面的代碼,$Rtm = '@[ Test Career 12](career:235)@[ Testing11](business:2)@[ Username](user:1)some text';if (preg_match("/@\[(.*?)\]\(user:(.*?)\)/", $Rtm, $match)) {    $Rtm0 = preg_replace("/@\[(.*?)\]\(user:(.*?)\)/", '<a href="/en/main/profile_page_link/$2">$1</a>, ', $Rtm);    $Rtm = rtrim($Rtm0, ', ');}if (preg_match("/@\[(.*?)\]\(business:(.*?)\)/", $Rtm, $match)) {    $slug = "1";    $Rtm01 = preg_replace("/@\[(.*?)\]\(business:(.*?)\)/", '<a href="/en/business/' . $slug . '/about">$1</a>, ', $Rtm);    $Rtm = rtrim($Rtm01, ', ');}if (preg_match("/@\[(.*?)\]\(career:(.*?)\)/", $Rtm, $match)) {    $slug = "2";    $Rtm02 = preg_replace("/@\[(.*?)\]\(career:(.*?)\)/", '<a href="/en/main/' . $slug . '/about">$1</a>, ', $Rtm);    $Rtm = rtrim($Rtm02, ', ');}echo $Rtm;上述代碼的輸出是,<a href="/en/main/profile_page_link/1"> Test Career 12](career:235)</a><a href="/en/business/1/about"> Testing11</a>, @[ Username, some text但我需要的輸出是,<a href="/en/main/2/about"> Test Career 12</a>, <a href="/en/business/1/about"> Testing11</a>, <a href="/en/main/profile_page_link/1"> Username</a> some text給定的字符串只是一個(gè)演示,順序可能會(huì)改變,因?yàn)樗莿?dòng)態(tài)的。但結(jié)構(gòu)是一樣的。如何獲得所需的輸出。我的編碼有問題嗎。
查看完整描述

2 回答

?
墨色風(fēng)雨

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超6個(gè)贊

為此,我將preg_replace_callback捕獲 中的文本[]、slug 類型(user、careerbusiness)和值(對(duì)于 slug?user)分組,并將它們傳遞到回調(diào)以形成 URL:

$Rtm = '@[ Test Career 12](career:235)@[ Testing11](business:2)@[ Username](user:1)some text';


$Rtm = preg_replace_callback('/@\[([^]]*)\]\(([a-z]+):([^)]*)\)/', function ($match) {

? ? switch($match[2]) {

? ? ? ? case 'user':

? ? ? ? ? ? return "<a href=\"/en/main/profile_page_link/$match[3]\">$match[1]</a>";

? ? ? ? ? ? break;

? ? ? ? case 'business':

? ? ? ? ? ? return "<a href=\"/en/business/1/about\">$match[1]</a>";

? ? ? ? ? ? break;

? ? ? ? case 'career':

? ? ? ? ? ? return "<a href=\"/en/main/2/about\">$match[1]</a>";

? ? ? ? ? ? break;

? ? ? ? default:

? ? ? ? ? ? return "";

? ? ? ? ? ? break;

? ? }

}, $Rtm);

echo $Rtm;

輸出(對(duì)于您的輸入字符串):


<a href="/en/main/2/about"> Test Career 12</a><a href="/en/business/1/about"> Testing11</a><a href="/en/main/profile_page_link/1"> Username</a>some text

3v4l.org 上的演示


查看完整回答
反對(duì) 回復(fù) 2023-06-24
?
BIG陽

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊

使用@\[([^][]*?)\]而不是@\[(.*?)\] 演示和解釋。

rtrim所有更換后僅執(zhí)行一次。

$Rtm = '@[ Test Career 12](career:235)@[ Testing11](business:2)@[ Username](user:1)some text';


if (preg_match("/@\[([^][]*?)\]\(user:(.*?)\)/", $Rtm, $match)) {

    $Rtm = preg_replace("/@\[([^][]*?)\]\(user:(.*?)\)/", '<a href="/en/main/profile_page_link/$2">$1</a>, ', $Rtm);

}


if (preg_match("/@\[([^][]*?)\]\(business:(.*?)\)/", $Rtm, $match)) {

    $Rtm = preg_replace("/@\[([^][]*?)\]\(business:(.*?)\)/", '<a href="/en/business/$2/about">$1</a>, ', $Rtm);

}


if (preg_match("/@\[([^][]*?)\]\(career:(.*?)\)/", $Rtm, $match)) {

    $Rtm = preg_replace("/@\[([^][]*?)\]\(career:(.*?)\)/", '<a href="/en/main/$2/about">$1</a>, ', $Rtm);

}

$Rtm = rtrim($Rtm, ', ');


echo $Rtm;


查看完整回答
反對(duì) 回復(fù) 2023-06-24
  • 2 回答
  • 0 關(guān)注
  • 205 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)