crazy sarah
crazy sarah

Reputation: 631

How to remove a repeating sequence from a string - php

I have a function written by someone else which returns the address of a customer. This function isn't as simple as you would have thought it might need to be. For a good reason it has been written so that the return variable goes through:

eval("\$address_out = \"$fmt\";");

first. And despite a quick look in the manual just what it does it a mystery to me. So the problem is the address is returned like these two examples (note on has 3 repeating <br/>s and the other 4)

ginger rogers<br /> hollywood<br /> California<br /> <br /> <br /> United states

ginger rogers<br /> hollywood<br /> <br /> <br /> <br /> United states

I thought it would be quicker to just strip the repeating <br/>s out but I'm confused because there can be a different amount of them and they are seperated by spaces.

Anyone any ideas?

EDIT: I should have said, I want to keep one of the
between each bit of the address, it's where there are multipul
I want to get rid of all but one

Upvotes: 0

Views: 219

Answers (1)

Patrick Moore
Patrick Moore

Reputation: 13344

$addr = str_replace( array( '<br/>', '<br>', '<br />', "\r", "\n" ), '', $addr ); will remove <br/> breaks, plus new line and carriage return characters.

To maintain one <br/> still:

while ( strpos( $addr, '<br /> <br />' ) !== false ) { 
    // while two adjoining <br />'s still exist in the variable
    // replace two with one <br /> break
    $addr = str_replace( '<br /> <br />', '<br />', $addr );
}

Upvotes: 1

Related Questions