Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 只有当url不';不要从http开始://_Php_Regex_Substitution - Fatal编程技术网

Php 只有当url不';不要从http开始://

Php 只有当url不';不要从http开始://,php,regex,substitution,Php,Regex,Substitution,试图在stackoverflow上找到答案,但我是一个完全的RegEx noob 我所需要的(如果可能的话)是在一些HTML中匹配一个PDF URL,如果它不是以http://开头,则在开头添加/content/,如果它是以http://开头,则不执行任何操作。您想要的正则表达式可能是 http://add/content/\S+?\.pdf 它说它必须从“开始”http://add/content/“然后,在最后出现.pdf文件之前,可以有任何不是空白的内容。 根据您使用的语言,您需要以不同

试图在stackoverflow上找到答案,但我是一个完全的RegEx noob


我所需要的(如果可能的话)是在一些HTML中匹配一个PDF URL,如果它不是以
http://
开头,则在开头添加
/content/
,如果它是以
http://
开头,则不执行任何操作。

您想要的正则表达式可能是

http://add/content/\S+?\.pdf
它说它必须从“开始”http://add/content/“然后,在最后出现.pdf文件之前,可以有任何不是空白的内容。 根据您使用的语言,您需要以不同的方式应用此功能。例如,在php中,它将是

preg_match_all('|http://add/content/\S+?\.pdf|',$html,$matches);
if(count($matches)) {
     //do stuff with the matches in the $matches array
} else {
     //there were no matches of that form
}

假设您想用javascript实现这一点

var links = document.getElementsByTagName("a");
for(var i = 0; i < links.length; i++){
    var link = links[i];
    var href = link.getAttribute("href");
    if(!/^http/.test(href))
    {
        link.setAttribute("href", "/content/" + href);
    }
}
var links=document.getElementsByTagName(“a”);
对于(变量i=0;i
这不需要正则表达式。您可以检查URL开头是否有您选择的语言中的文本
http://
,如果没有,请在其中添加
content/
。您还没有指定要使用的平台,是使用javascript还是服务器端?感谢代码-我想做服务器端,消除在客户端启用JS的需要。不过谢谢你:)