如何替换<a></a>之间的链接并保持href不变?

时间:2019-04-10 06:44:58

标签: php regex

如何替换之间的链接,并保持href不变?

这是我的代码:

$str="Lorem Ipsum.<a href='https://test.be/assets/kcfinder/upload/files/certificat_auto.pdf'>https://test.be/assets/kcfinder/upload/files/certificat_auto.pdf</a>Lorem Ipsum ";

$pattern = '/>https:\/\/test.be\/assets\/kcfinder\/upload\/files\/.*\./';
$replacement = '>${1}';

echo preg_replace($pattern, $replacement, $str);

这是输出:

Lorem Ipsum.<a href='https://test.be/assets/kcfinder/upload/files/certificat_auto.pdf'>pdf</a>Lorem Ipsum 

我需要输出为:

Lorem Ipsum.<a href='https://test.be/assets/kcfinder/upload/files/certificat_auto.pdf'>certificat_auto.pdf</a>Lorem Ipsum 

我的替换操作有问题,模式匹配

1 个答案:

答案 0 :(得分:0)

您可能会从中得到一些想法。

public class RemotePost
{
    private Dictionary<string, string> Inputs = new Dictionary<string, string>();
    public string Url = "";
    public string Method = "post";
    public string FormName = "form1";
    public StringBuilder strPostString;

    public void Add(string name, string value)
    {
        Inputs.Add(name, value);
    }
    public void generatePostString()
    {
        strPostString = new StringBuilder();

        strPostString.Append("<html><head>");
        strPostString.Append("</head><body onload=\"document.form1.submit();\">");
        strPostString.Append("<form name=\"form1\" method=\"post\" action=\"" + Url + "\" >");

        foreach (KeyValuePair<string, string> oPar in Inputs)
            strPostString.Append(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", oPar.Key, oPar.Value));

        strPostString.Append("</form>");
        strPostString.Append("</body></html>");
    }
    public void Post()
    {
        System.Web.HttpContext.Current.Response.Clear();
        System.Web.HttpContext.Current.Response.Write(strPostString.ToString());
        System.Web.HttpContext.Current.Response.End();
    }
}

https://rubular.com/r/b46dQ9G27C4A3A