Reputation: 566
I have the following output:
Item
Length : 130
Depth : 25
Total Area (sq cm): 3250
Wood Finish: Beech
Etc: etc
I want to remove the Total Area (sq cm): and the 4 digits after it from the string, currently I am trying to use str_replace like so:
$tidy_str = str_replace( $totalarea, "", $tidy_str);
Is this the correct function to use and if so how can I include the 4 random digits after this text? Please also note that this is not a set output so the string will change position within this.
Upvotes: 0
Views: 2699
Reputation: 7552
You are looking for substr_replace
:
$strToSearch = "Total Area (sq cm):";
$totalAreaIndex = strpos($tidy_str, $strToSearch);
echo substr_replace($tidy_str, '', $totalAreaIndex, strlen($strToSearch) + 5); // 5 = space plus 4 numbers.
If you want to remove the newline too, you should check if it's \n or \r\n. \n add one, \r\n add two to offset. Ie. strlen($strToSearch) + 7
Upvotes: 1
Reputation: 40861
You can practice php regex at http://www.phpliveregex.com/
<?php
$str = '
Item
Length : 130
Depth : 25
Total Area (sq cm): 3250
Wood Finish: Beech
Etc: etc
';
echo preg_replace("/Total Area \(sq cm\): [0-9]*\\n/", "", $str);
Item Length : 130 Depth : 25 Wood Finish: Beech Etc: etc
Upvotes: 1
Reputation: 1367
This will do it.
$exp = '/\(sq cm\): \d+/';
echo preg_replace($exp, '', $array);
Upvotes: 0
Reputation: 2029
Try with this:
preg_replace('/(Total Area \(sq cm\): )([0-9\.,]*)/' , '', $tidy_str);
Upvotes: 0