/    Sign up×
Community /Pin to ProfileBookmark

explode string with tag

I have string which looks like this: “<p>some text</p>another text”

I want split it to: “<p>some text</p>” and “another text”

When I try:

[code=php]
$string = “<p>some text</p>anothertext”;
$split = explode(“</p>”, $string);
echo $split[0];
echo $split[1];
[/code]

I got “<p>some text” with missing “</p>” at the end. How can I fix it?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@andre4s_yFeb 02.2009 — If it alright to use regex, then maybe this code will help you :
[code=php]
<?php
$string = "<p>some text</p>anothertext";
if(preg_match("#(<p>.+?</p>)(.+)#",$string,$output))
{
echo $output[1];
echo $output[2];
}
else
{
echo "pattern is not recognized";
}
?>
[/code]

Hope this help
Copy linkTweet thisAlerts:
@andre4s_yFeb 02.2009 — If you face this kind of pattern :
[CODE]$string = "test <p>some text</p>anothertext<p>some text 2</p>another text2<p>sometext3</p>";[/CODE]
Then may be this code help you :
[code=php]
<?php
$string = "test <p>some text</p>anothertext<p>some text 2</p>another text2<p>sometext3</p>";
if(preg_match_all("#(<p>.+?</p>)((?:.(?!p>))*)#",$string,$output))
{
print_r($output);
}
else
{
echo "pattern is not recognized";
}
?>
[/code]

$output[1] contain array that each value has pattern <p>text</p>

$output[2] contain array that each value has pattern any kind of character that is not followed by p>.

Hope these help
Copy linkTweet thisAlerts:
@HelleshternauthorFeb 02.2009 — Yes, it helps. Thank you ?
×

Success!

Help @Helleshtern spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...