Jump to content

Preg_Replace Help


balkan7

Recommended Posts

That first bracket hasn't been escaped, the second forward slash hasn't been escaped... And I would fix that middle function so it's a lazy search rather than a greedy search. (and there's a board for this btw :-P)

 

<?php function clearbb($text) {
  //youtube mod
  if (preg_match("/\[youtube\]([^\[]*)\[\/youtube\]/", $text)) {
  $text = preg_replace('', '', $text);
}
} ?>

 

Here's what that middle part means:

 

([^\[]*)

 

All text up to "[" will be included.

 

So, joajsfojsojfaoisjfoijsdjafjsdoifjoasdfa , it will find first, store everything after that up to the first "[", find and give you the results.

Link to comment
Share on other sites

Oh, I see how you're doing it... You're using an if/else statement to see if there's at least one match for the youtube pattern, then replacing it :-P

 

preg_replace requires that same pattern inside it to find and replace... :-o

 

 

<?php function clearbb($text) {
$pattern = "/\[youtube\]([^\[]*)\[\/youtube\]/";

  //youtube mod
  if (preg_match($pattern, $text)) {
  $text = preg_replace($pattern, '', $text);
}
} ?>

 

See if THAT works.

Link to comment
Share on other sites

You were eating up too much with the * symbol, so I made it lazy by adding a ? should work now.

Try this yes It re something turn ok?

<?php
function clearbb($text) {
     //youtube mod
     $text = preg_replace("/\[youtube\](.*?)\[\/youtube\]/","", $text);
     return $text;
}
?>

 

However, that regex will wipe out the entire string of

lalalala! look text here, not for long!

If you just want to take out the tags themselves, try this:

$text = preg_replace('~\[youtube\](.*?)\[/youtube\]~', '$1', $text);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.