Jump to content

Finding and removing numbers (1-25) from a string ...


AshleyQuick

Recommended Posts

%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

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.

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.