TransWikia.com

Get the string before replace

Stack Overflow Asked by Tanker on February 9, 2021

How can I get the word that I’m about to replace?…

<?php
$string = "Delivery (fail) with error 500";
$words = ['fail', 'error', 'abuse'];
$exist = ($string != str_ireplace($words,"XX",$string)) ? true: false;
if($exist) {
// Do some magic...
}

This has been the most "elegant" way that I could find if string has a particular word, but how do I know which of the 3 or 4 or xN words is the one that is begin replace…
If the string has 2 or 3 of the array words I only care for one it doesn’t matter if the first or last as long as I know which of the word in the array where fund…
Does any one know how to do it with out a foreach or for or while or any loop for that matter…
Thank you in advance…

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP