Dr-Neo Posted May 22, 2008 Share Posted May 22, 2008 Hello All, I have a problem with String manipulation : $string = "Hello And welcome To ^#phpfreaks# the best php forum"; how to echo the phpfreak word only? i mean how to extract it from the variable? i need to know how to extract words between 2 symbols even the symbols is *&^%$? thank you Link to comment https://forums.phpfreaks.com/topic/106782-solved-string-manipulation/ Share on other sites More sharing options...
DyslexicDog Posted May 22, 2008 Share Posted May 22, 2008 Will the set of symbols always be the same? Link to comment https://forums.phpfreaks.com/topic/106782-solved-string-manipulation/#findComment-547377 Share on other sites More sharing options...
rhodesa Posted May 22, 2008 Share Posted May 22, 2008 Lot's of ways...off the top of my head, here are two: <?php $string = "Hello And welcome To ^#phpfreaks# the best php forum"; $s = strpos($string,'#'); print substr($string,$s+1,strpos($string,'#',$s+1) - $s - 1); ?> <?php $string = "Hello And welcome To ^#phpfreaks# the best php forum"; if(preg_match('/#(.+?)#/',$string,$matches)) print $matches[1]; ?> Link to comment https://forums.phpfreaks.com/topic/106782-solved-string-manipulation/#findComment-547378 Share on other sites More sharing options...
soycharliente Posted May 22, 2008 Share Posted May 22, 2008 <?php $string = "Hello And welcome To ^#phpfreaks# the best php forum"; $char = "#"; $front = strpos($string, $char) + strlen($char); $back = strpos($string, $char, $front); echo substr($string, $front, $back - $front); ?> Link to comment https://forums.phpfreaks.com/topic/106782-solved-string-manipulation/#findComment-547380 Share on other sites More sharing options...
effigy Posted May 22, 2008 Share Posted May 22, 2008 This may work, depending on your entire data set: <pre> <?php $data = <<<DATA Hello And welcome To ^#phpfreaks# the best php forum Hello And welcome To ^#phpfreaks& the best php forum Hello And welcome To %#phpfreaks# the best php forum Hello And welcome To phpfreaks$ the best php forum Hello And welcome To ^#phpfreaks# the best php forum Hello And welcome To ^^phpfreaks^ the best php forum DATA; preg_match_all('/[\p{S}\p{P}]+(.+?)[\p{S}\p{P}]+/', $data, $matches); print_r($matches); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/106782-solved-string-manipulation/#findComment-547384 Share on other sites More sharing options...
Dr-Neo Posted May 23, 2008 Author Share Posted May 23, 2008 thank you so much all the closet solution for me is <?php $string = "Hello And welcome To ^#phpfreaks# the best php forum"; $s = strpos($string,'#'); print substr($string,$s+1,strpos($string,'#',$s+1) - $s - 1); ?> but the problem that its make it for once. if the text like that $string = "Hello And welcome To ^#phpfreaks# the best php forum and the ^#yahoo# is the best email and my ^#facebook# is the best site"; it will only print phpfreaks and i want print all of the results could you please help me to loop through it thank you so much Link to comment https://forums.phpfreaks.com/topic/106782-solved-string-manipulation/#findComment-548354 Share on other sites More sharing options...
rhodesa Posted May 23, 2008 Share Posted May 23, 2008 <?php $string = "Hello And welcome To ^#phpfreaks# the best php forum and the ^#yahoo# is the best email and my ^#facebook# is the best site"; if(preg_match_all('/\^#(.+?)#/',$string,$matches)){ foreach($matches[1] as $match) print $match; } ?> Link to comment https://forums.phpfreaks.com/topic/106782-solved-string-manipulation/#findComment-548362 Share on other sites More sharing options...
Dr-Neo Posted May 24, 2008 Author Share Posted May 24, 2008 thank you so much for your post. <?php $string = "Hello And welcome To ^#phpfreaks# the best php forum and the ^#yahoo# is the best email and my ^#facebook# is the best site"; if(preg_match_all('/\^#(.+?)#/',$string,$matches)){ foreach($matches[1] as $match) print $match; } ?> this code posted before by effigy and it give me a lot of errors when i tried to change the symbols so i used this code <?php $string = "Hello And welcome To ^#phpfreaks# the best php forum"; $s = strpos($string,'#'); print substr($string,$s+1,strpos($string,'#',$s+1) - $s - 1); ?> and its working just like dream. the problem when i need use more than one time is there anyway to use the same code for than one time in the same variable? Link to comment https://forums.phpfreaks.com/topic/106782-solved-string-manipulation/#findComment-548733 Share on other sites More sharing options...
minidak03 Posted May 24, 2008 Share Posted May 24, 2008 Here you go <?php $string = "Hello And welcome To ^#phpfreaks# the best php forum and the ^#yahoo# is the best email and my ^#facebook# is the best site"; $pattern = '/#(.*?)#/'; preg_match_all($pattern,$string,$data,PREG_SET_ORDER); $a = 0; $count = count($data); while($a < $count) { echo $data[$a][1].'<br />'; $a++; } ?> Link to comment https://forums.phpfreaks.com/topic/106782-solved-string-manipulation/#findComment-548734 Share on other sites More sharing options...
Dr-Neo Posted May 28, 2008 Author Share Posted May 28, 2008 Thank you so much all. and thank you minidak03. <?php $string = "Hello And welcome To ^#phpfreaks# the best php forum and the ^#yahoo# is the best email and my ^#facebook# is the best site"; $pattern = '/#(.*?)#/'; preg_match_all($pattern,$string,$data,PREG_SET_ORDER); $a = 0; $count = count($data); while($a < $count) { echo $data[$a][1].'<br />'; $a++; } ?> this is the one problem resolved Link to comment https://forums.phpfreaks.com/topic/106782-solved-string-manipulation/#findComment-551544 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.