senyo Posted January 15, 2010 Share Posted January 15, 2010 I am trying to remove all the text between this tags <begin>data something something more<end> what should I use, so that all the text is delete? Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/ Share on other sites More sharing options...
JAY6390 Posted January 15, 2010 Share Posted January 15, 2010 $text = preg_replace('/<login>.*?<end>/', '<login><end>', $text); Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/#findComment-995745 Share on other sites More sharing options...
senyo Posted January 15, 2010 Author Share Posted January 15, 2010 it doesn't work the text is still there Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/#findComment-995747 Share on other sites More sharing options...
JAY6390 Posted January 15, 2010 Share Posted January 15, 2010 $text = preg_replace('/<begin>.*?<end>/', '<begin><end>', $text); Oops for some bizarre reason I put login instead of begin. That should work Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/#findComment-995759 Share on other sites More sharing options...
senyo Posted January 15, 2010 Author Share Posted January 15, 2010 that is not the problem, it doesn't remove the text with the right tags Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/#findComment-995761 Share on other sites More sharing options...
salathe Posted January 15, 2010 Share Posted January 15, 2010 Be more specific with what you want to find/remove, what the bigger piece of text is and what exactly you want the end result to be. We're not mind readers. Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/#findComment-995763 Share on other sites More sharing options...
JAY6390 Posted January 15, 2010 Share Posted January 15, 2010 Works for me. Either you're using the wrong variable to it or the text isn't as you described. Like salathe says you need to give more info Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/#findComment-995765 Share on other sites More sharing options...
senyo Posted January 15, 2010 Author Share Posted January 15, 2010 For this tags it works but for other tags like <div class=title> data <div class='end'> it doesn't Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/#findComment-995775 Share on other sites More sharing options...
JAY6390 Posted January 15, 2010 Share Posted January 15, 2010 So you want anything between ANY tags removed completely??? Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/#findComment-995777 Share on other sites More sharing options...
senyo Posted January 15, 2010 Author Share Posted January 15, 2010 yes Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/#findComment-995779 Share on other sites More sharing options...
JAY6390 Posted January 15, 2010 Share Posted January 15, 2010 oh, in that case I'd use $text = preg_replace('/>.*?</m', '><', $text); Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/#findComment-995781 Share on other sites More sharing options...
senyo Posted January 15, 2010 Author Share Posted January 15, 2010 how would I make it remove from this tags <div class=title> data <div class='end'>, I want make it delete the text between the two tags, and for other tags if I change them Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/#findComment-995791 Share on other sites More sharing options...
senyo Posted January 15, 2010 Author Share Posted January 15, 2010 maybe is something wrong with my text, because it finds the tags but it doesn't removes the text Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/#findComment-995820 Share on other sites More sharing options...
senyo Posted January 15, 2010 Author Share Posted January 15, 2010 are there other ways of searching and deleting text from a string? Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/#findComment-995885 Share on other sites More sharing options...
x_filed2k Posted January 16, 2010 Share Posted January 16, 2010 Can't you use string_replace(); Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/#findComment-995912 Share on other sites More sharing options...
x_filed2k Posted January 16, 2010 Share Posted January 16, 2010 Using preg_match() alone will not remove any string in between the <login> and <end>. Test your codes again but this time put a string in-between the <login> and <end>, and var_dump it, the result will be the string that you wanna remove. Try this one.. <?PHP $pattern = "/<login>.*?<end>/"; $sample_value = "<login>any string here<end>"; preg_match($pattern, $sample_value, $text); echo("This is the result when you only used the preg_match().<br />"); var_dump($text); echo("<br /><br />"); $text = str_replace($text, "<login><end>", $sample_value); echo("You have used the str_replace() to remove the unwanted strings.<br />"); var_dump($text); echo("<br /><br />"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/#findComment-995924 Share on other sites More sharing options...
JAY6390 Posted January 16, 2010 Share Posted January 16, 2010 preg_replace is what was used. and it works fine for the tags. The OP was trying to remove everything between tags, not capture it Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/#findComment-995933 Share on other sites More sharing options...
x_filed2k Posted January 16, 2010 Share Posted January 16, 2010 Waaa.. I thought it was preg_match... that was what I've read. Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/#findComment-995934 Share on other sites More sharing options...
x_filed2k Posted January 16, 2010 Share Posted January 16, 2010 Sorry for my mistake. Well I tried to do it... Here's the code... $text = preg_replace('/<div class=title>.*?<div class=\'end\'>/', '<div class=title><div class=\'end\'>', $text); Quote Link to comment https://forums.phpfreaks.com/topic/188606-remove-text-from-a-string/#findComment-995937 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.