Jump to content

Converting to a String for preg_replace


joev

Recommended Posts

I am trying to create a string which lists receipt numbers that are separated by a commas. I want to remove the last comma after the last receipt number for display:

 

$receipt = "";
for ($i = 0; $i < 3; $i++){
	$receiptID =  $i;
	$receipt = settype ($receiptID, string) . ", ";
	//strval ($receipt);
}
//$receipt = "325, 326,";
$receipt = preg_replace('/,$/', '', $receipt); //Remove last comma

echo $receipt;

 

If I hard code $receipt between double quotes, my preg_replace works fine. If I build $receipt from the for loop, preg_replace leaves the last comma. The problem, I believe, is because $receipt from the loop is not being evaluated as a string. I have tried setting $receipt to a string with settype(), strval(), and (string) without any luck.

 

What am I missing here?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/61589-converting-to-a-string-for-preg_replace/
Share on other sites

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.