Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

php - an opposite function or code to preg_match_all

here i have a built in php function that in this example i am using to turn the content inside a span tag into an array.

preg_match_all('/<span.*?</span>/', $var, $newVar);

is it possible to kind of place into an array the other text between the span tags.

<p>i am a sentence <span id="blah"> im content inside of the span </span> im another sentence <span id="anId">i m another span content</span> im the last sentence in this p tag <span id="last">im the third span tag in this p tag<span></p>

more specifically the above function will output the above p tag as an array of span tags with just the text inside and no tags around it wich is good step one accomplished it would look something like this

array ([0] => im content inside of the span [1] => i m another span content [2] => im the third span tag in this p tag )

this is the text inside the span tags, i want to use these to implode the string.

, now i want to do the opposite and have the strings in between them as an array.

i want to get an output of

array ([0] => i am a sentence [1] => im another sentence [2] => im the last sentence in this p tag )

this is the text outside of the span tags Surely this is possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use preg_split(), using a regular expression that matches a tag as the delimiter.

$array = preg_split('/<[^>]*>/', $var);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...