balkan7 Posted January 9, 2008 Share Posted January 9, 2008 i need help if find text replace whit blank but this function not working blank all text. <?php function clearbb($text) { //youtube mod if (preg_match("/[youtube\](.*?)\[/youtube\]/", $text)) { $text = preg_replace('', '', $text); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85260-preg_replace-help/ Share on other sites More sharing options...
kratsg Posted January 9, 2008 Share Posted January 9, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85260-preg_replace-help/#findComment-434956 Share on other sites More sharing options...
balkan7 Posted January 9, 2008 Author Share Posted January 9, 2008 i have tried but again whitout result i dont know why, not show me included text like: bla bla sdfdf now everything is blank Quote Link to comment https://forums.phpfreaks.com/topic/85260-preg_replace-help/#findComment-434970 Share on other sites More sharing options...
kratsg Posted January 9, 2008 Share Posted January 9, 2008 What's the purpose of that last line? $text = preg_replace('', '', $text); Quote Link to comment https://forums.phpfreaks.com/topic/85260-preg_replace-help/#findComment-434974 Share on other sites More sharing options...
balkan7 Posted January 9, 2008 Author Share Posted January 9, 2008 What's the purpose of that last line? $text = preg_replace('', '', $text); code for replace string for news, so text of news now are blank i need text for news show bbcode of youtube not show Quote Link to comment https://forums.phpfreaks.com/topic/85260-preg_replace-help/#findComment-434981 Share on other sites More sharing options...
kratsg Posted January 10, 2008 Share Posted January 10, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85260-preg_replace-help/#findComment-435072 Share on other sites More sharing options...
balkan7 Posted January 11, 2008 Author Share Posted January 11, 2008 i have tried again whitout result not show nothing, news text, youtube bbcode. Quote Link to comment https://forums.phpfreaks.com/topic/85260-preg_replace-help/#findComment-436464 Share on other sites More sharing options...
Ken2k7 Posted January 11, 2008 Share Posted January 11, 2008 Dude, your sentences are poorly written. Try this: <?php function clearbb($text) { //youtube mod $text = preg_replace("/\[youtube\](.*)\[\/youtube\]/","", $text); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85260-preg_replace-help/#findComment-436570 Share on other sites More sharing options...
balkan7 Posted January 11, 2008 Author Share Posted January 11, 2008 yes this is propertly written but wont working to my script, i try whit another way. thanks for support... Quote Link to comment https://forums.phpfreaks.com/topic/85260-preg_replace-help/#findComment-436577 Share on other sites More sharing options...
GingerRobot Posted January 11, 2008 Share Posted January 11, 2008 Well, apart from anything else - your function doesn't return anything, or echo anything. So making a call to this function wont actually do anything. Quote Link to comment https://forums.phpfreaks.com/topic/85260-preg_replace-help/#findComment-436579 Share on other sites More sharing options...
dsaba Posted January 11, 2008 Share Posted January 11, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/85260-preg_replace-help/#findComment-436814 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.