/    Sign up×
Community /Pin to ProfileBookmark

str_replace problem

Hi,

I have a script that replaces html tags with BB code, however I am getting a problem when doing the <li> tags.

if I do this, the code works:

[code=php]$bb_map = array(“<li>” => ‘[LI]’);

$string = str_replace(array_keys($bb_map), $bb_map, $string);[/code]

However if I do this the code does not work:

[code=php]$bb_map = array(“</p><li>” => ‘[LI]’);

$string = str_replace(array_keys($bb_map), $bb_map, $string);[/code]

Any ideas

Thanks in advance,

Ben

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NihilisteOct 02.2009 — Hi,

The only thing I can see is that in the second case you are looking for: "</p><li>" not only "<li>". So maybe your $string var doesn't contains the "</p><li>" string.

If you want a more flexible way to do it, you should use preg_replace instead of str_replace.

Something like this should do the job:

[code=php]
$bb_map = array( '/(</p>)?<li>/' => '[LI]' );

$string = preg_replace( array_keys( $bb_map ), $bb_map, $string );
[/code]
Copy linkTweet thisAlerts:
@Benji6996authorOct 02.2009 — Yes that works, thanks very much.
×

Success!

Help @Benji6996 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...