0

I have string and I want to splitt it to array and remove a specific part and then convert it back to a string .

$string = "width,100;height,8600;block,700;narrow,1;"

I want to search block in this string and remove it with its value "block,700;" and get the string as "width,100;height,8600;narrow,1;"

below is my code php which i tried.Please advice me

                $outerARR = explode(";", $string);
                $arr = array();
                foreach ($outerARR as $arrvalue) {
                    $innerarr = explode(",", $arrvalue);
                    $arr[] = $innerarr;
                }
                for ($i = 0; $i < count($arr); $i++) {
                    if (in_array($arr, 'block')) {
                      unset($arr[$i]);

                    }
                }

Please note that "block" in aboive string will not always contain and the value may differ. So I can't use string replace . please advice

2
  • $string = str_replace('block,700;', '', $string); Commented Mar 22, 2017 at 16:09
  • @Fky :"block: wil not always include in the string , and the value may differ. please advice. Commented Mar 22, 2017 at 16:13

2 Answers 2

1

You essentially want to replace part of your string:

$string = "width,100;height,8600;block,700;narrow,1;";
$regex = '#(block,(.*?);)#';
$result = preg_replace($regex, '', $string);
echo $result;
Sign up to request clarification or add additional context in comments.

1 Comment

"block: wil not always include in string , and the value may differ. how to proceed
1

Try this:

$string = "width,100;height,8600;block,700;narrow,1;"
$outerARR = explode(";", $string);
$arr = array();
foreach ($outerARR as $arrvalue) {
    $innerarr = explode(",", $arrvalue);
    $arr[$innerarr[0]] = $innerarr[1];
}
if (array_key_exists('block', $arr)) {
    unset($arr['block']);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.