Prge_replace is not working properly in php

Prge_replace is not working properly in php



I am trying to replace the below string


`$test_numbers_4 = '\;\3;4;5';



to



3,4,5


3,4,5



Here is my code


$test_numbers_4 = '\;\3;4;5';
printf( "<h4>Task 4:</h4>n" );
$str = preg_replace('/s0,/', '', $test_numbers_4);
$str1 = preg_replace('/\\\|;\\/', ',', $str);
$array = explode(',', $str1);
$res=implode( ',', $array) ;
echo $res;



Output: 3,4,5


3,4,5


Edit:



I have a string like this $string =\,\2,7,-3,5,-2 if string is like this when i run the file i would like to show error message saying that negative numbers -3,-2 not allowed.


$string =\,\2,7,-3,5,-2


negative numbers -3,-2 not allowed.



But it's not giving me the result what I expected



Can anyone help me what is the exact problem?




2 Answers
2



It looks like you're trying to throw away things that are not numbers or ;, in which case you can do that with this code. It uses preg_replace to remove unwanted characters then preg_split to split into an array on ;, with the PREG_SPLIT_NO_EMPTY flag to ensure no empty values in the array.


;


preg_replace


preg_split


;


PREG_SPLIT_NO_EMPTY


$test_numbers_4 = '\;\3;4;5';
$str = preg_replace('/[^d;]/', '', $test_numbers_4);
$array = preg_split('/;/', $str, -1, PREG_SPLIT_NO_EMPTY);
$res=implode(',', $array) ;
echo $res;



Output:


3,4,5



If you specifically want to just remove spaces and double backslashes, then use this preg_replace:


$str = preg_replace('/\\|s+/', '', $test_numbers_4);





Yup its working and i have one more doubt similart to this can i ask
– suresh
Sep 2 at 6:22





If it's part of the same question then just edit your question. If not, you should ask a new question.
– Nick
Sep 2 at 6:23





its a part of question only about preg_replace only
– suresh
Sep 2 at 6:24





Go for it then! :)
– Nick
Sep 2 at 6:24





i have string like this $string =\,\ 2,7,-3,5,-2 if it is like this i would throw error message when i run the file saying that negative numbers -3,-2 not allowed
– suresh
Sep 2 at 6:25



You can try this steps to get what you need-



implode it with comma using implode()



DEMO: https://3v4l.org/IKoYt



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)