如何应用正则表达式来过滤字符串
问题描述:
How can I apply regular expression to filter only
[video src="http://duel.evotechaustin.com/wp-content/uploads/2010/09/kramer.m4v" width="480" height="360" id="b-test" class="player" ]
from the following string
Is simply dummy text of the printing and typesetting industry.
[video src="http://duel.evotechaustin.com/wp-content/uploads/2010/09/kramer.m4v" width="480" height="360" id="b-test" class="player" ]
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.
or Without using regular expression and Dom is there anyways to get the same
如何应用正则表达式仅过滤 p>
[ video src =“http://duel.evotechaustin.com/wp-content/uploads/2010/09/kramer.m4v”width =“480”height =“360”id =“b-test”class =“player” ]
code> pre>
来自以下字符串 p>
只是打印和排版行业的虚拟文本。
[video src =“http://duel.evotechaustin.com/wp-content/uploads/2010/09/kramer.m4v”width =“480”height =“360”id =“b-test”class = “玩家”]
定律自16世纪以来,Ipsum一直是业界标准的虚拟文本。
code> pre>
或者没有使用正则表达式而且Dom无论如何都得到相同的 p>
div>
答
preg_match("/\[video[^\]]+\]/i", $subject, $matches);
$matches[0]
contains
[video src="http://duel.evotechaustin.com/wp-content/uploads/2010/09/kramer.m4v" width="480" height="360" id="b-test" class="player"]
答
That's a shortcode and you can easily use the WordPress Shortcode API to handle those shortcodes:
function video_shortcode( $atts, $content = null ) {
// do whatever you want to to
}
add_shortcode('video', 'video_shortcode');
In the $atts
array you will have a list of all of your attributes from the video shortcode:
array(
"src" => "http://duel.evotechaustin.com/wp-content/uploads/2010/09/kramer.m4v",
"width" => "480",
"height" => "360",
"id" => "b-test",
"class" => "player"
)
答
Use substring and indexOf
for example,
htmlString = document.getElementById('div').innerHtml();
startIndex = htmlString.indexOf("[video");
endIndex = htmlString.indexOf("]", startIndex);
output = htmlString.substring(startIndex, endIndex);