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
https://forums.phpfreaks.com/topic/85260-preg_replace-help/#findComment-434956
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
https://forums.phpfreaks.com/topic/85260-preg_replace-help/#findComment-435072
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
https://forums.phpfreaks.com/topic/85260-preg_replace-help/#findComment-436814
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.