AshleyQuick Posted September 26, 2011 Share Posted September 26, 2011 %3$s displays a string of text that always begins with a number followed by a colon (from 1-25 only). 1: 2: 3: etc... Is there a way to remove the number and the colon entirely and leave the rest of the string intact? function wp_rss( $url, $num_items = -1 ) { if ( $rss = fetch_rss( $url ) ) { if ( $num_items !== -1 ) { $rss->items = array_slice( $rss->items, 0, $num_items ); } //start count $i = 1; foreach ( (array) $rss->items as $item ) { printf( '<div title="%2$s"><strong>'.$i.'</strong></div><div>%3$s</div>', esc_url( $item['link'] ), esc_attr( strip_tags( $item['description'] ) ), htmlentities( $item['title'] ) ); //increment $i++; } Ashley Link to comment https://forums.phpfreaks.com/topic/247875-finding-and-removing-numbers-1-25-from-a-string/ Share on other sites More sharing options...
codefossa Posted September 26, 2011 Share Posted September 26, 2011 <?php $string = '15:Some text.'; $newString = preg_replace('/([0-9]*?)\:/', '', $string, 1); echo $newString; ?> Link to comment https://forums.phpfreaks.com/topic/247875-finding-and-removing-numbers-1-25-from-a-string/#findComment-1272808 Share on other sites More sharing options...
silkfire Posted September 26, 2011 Share Posted September 26, 2011 $string = '15:Some text.'; $string = array_pop(explode(':', $string, 1)); echo $string; Link to comment https://forums.phpfreaks.com/topic/247875-finding-and-removing-numbers-1-25-from-a-string/#findComment-1272809 Share on other sites More sharing options...
codefossa Posted September 26, 2011 Share Posted September 26, 2011 $string = '15:Some text.'; $string = array_pop(explode(':', $string, 1)); echo $string; Would be .. <?php $string = '15:Some text.'; $string = array_pop(explode(':', $string, 2)); echo $string; ?> Link to comment https://forums.phpfreaks.com/topic/247875-finding-and-removing-numbers-1-25-from-a-string/#findComment-1272812 Share on other sites More sharing options...
silkfire Posted September 26, 2011 Share Posted September 26, 2011 Omg yes I meant to write a 2 even had it in my test code =) My fault. Link to comment https://forums.phpfreaks.com/topic/247875-finding-and-removing-numbers-1-25-from-a-string/#findComment-1272813 Share on other sites More sharing options...
codefossa Posted September 26, 2011 Share Posted September 26, 2011 Omg yes I meant to write a 2 even had it in my test code =) My fault. I used preg_replace() to confirm it was a number being removed though. I'm not sure they would want a string such as "Some Text: And More Text" to remove everything up until the first colon. If every string has the number before it though, then either way would be fine. Link to comment https://forums.phpfreaks.com/topic/247875-finding-and-removing-numbers-1-25-from-a-string/#findComment-1272815 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.