简体   繁体   English

在项目src中查找URL的基本路径

[英]Finding base path of URL's in item src

I need to search for 我需要搜寻

src="http://www.domain.com/wp-content/uploads/2010/12/6a00e54fa8abf788330147e0622c2e970b-800wi.jpg"

and get 并得到

"http://www.domain.com/wp-content/uploads/2010/12/"

Since I never know how long the string is I need to get everything up to the last / 由于我永远不知道字符串多长时间,因此我需要将所有内容都保留到最后一个/

have a look at pathinfo() 看看pathinfo()

ETA due to question edit: 由于问题编辑的预计到达时间:

@Chris: what you're trying to do is parse the DOM and pull out the values of src attributes. @Chris:您要尝试的是解析DOM并提取src属性的值。 The easiest and most foolproof way to do this is to use a DOM parser. 最简单,最简单的方法是使用DOM解析器。 Have a look at DomDocument::loadHTML and the rest of the DOMDocument class. 看一下DomDocument::loadHTMLDOMDocument类的其余部分。

使用stripos()

$val = substr($url, 0, stripos($url, '/')); 

how about this: 这个怎么样:

$str = 'src="http://www.domain.com/wp-content/uploads/2010/12/6a00e54fa8abf788330147e0622c2e970b-800wi.jpg"';
$str = substr($str,5);
$str = substr($str,0,-1);
echo dirname($str)."/";

You can loop the string in reverse and look for the 1st '\\' that you encounter, mark it's position, then copy the string up to that point into another string. 您可以反向循环该字符串,寻找遇到的第一个“ \\”,标记它的位置,然后将字符串复制到该位置再复制到另一个字符串中。

$resulted_path = "";    
for($i = strlen($str)-1 ; ($i>=0) && ($str{$i} != '/') ; $i --);
strncpy($resulted_path, $str, $i);

Do not forget the ';' 不要忘记“;” after the for loop. 在for循环之后。 The resulting string should contain your desired string. 结果字符串应包含所需的字符串。

References: charAt | 参考: charAt | strncpy | strncpy | strlen strlen的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM