Wednesday, 15 January 2014

extract first image and removed the first image from string -



extract first image and removed the first image from string -

i have string has both text , images remove first image using php.

$string = 'this test <img src="link_to_image1">, other text. <img src="link_to_another_image" border="0">'; $str = preg_replace('/\<img src=\"[aa-zz0-9\/\_\.]+\"\>/','',$string, 1);

to using callback. might little more flexible in case you're wanting stuff in future based on images @ different positions in string

class imgreplacer { function cb($matches){ if (!$this->counter){ $this->counter++; homecoming ''; } else { homecoming $matches[0]; } } } $ir = new imgreplacer; $ir->counter = 0; $string = 'this test <img src="link_to_image1">, other text. <img src="link_to_another_image" border="0">'; $string = preg_replace_callback( '#(<img.*?>)#', array(&$ir, 'cb'), $string); echo $string; test , other text. <img src="link_to_another_image" border="0">

image

No comments:

Post a Comment