how to remove specific characters and special characters from a string in php [closed]
I Have a string data coming from for loop, i want to remove specific characters and special characters from a string.Please find below my data.
["rn rn 300518</departureDate>rnttttttttt
rnttttttttt rn </availabilityDetails>rn
rn HYD</cityAirport>rn </departureLocationInfo>rn
rn DXB</cityAirport>rn </arrivalLocationInfo>rn
</availabilityProductInfo>702</orderClassesByCabin>
</cabinOption>",44,"AUHOT3116"]
i tried str_replace and trim functions in php but not working.I want out put from this string like below.
300518-departuredate-availabilityDetails-HYD-cityAirport-departureLocationInfo-DXB-
cityAirport-arrivalLocationInfo-availabilityProductInfo-702-
orderClassesByCabin-44-AUHOT3116
simply remove all ,r,n,t from this string. Can any one will help me.I was tried not getting result.Thanks advance..
php
closed as off-topic by Paul, Rob, Billal Begueradj, lucascaro, Devon_C_Miller Nov 12 '18 at 5:18
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Paul, Rob, Billal Begueradj
add a comment |
I Have a string data coming from for loop, i want to remove specific characters and special characters from a string.Please find below my data.
["rn rn 300518</departureDate>rnttttttttt
rnttttttttt rn </availabilityDetails>rn
rn HYD</cityAirport>rn </departureLocationInfo>rn
rn DXB</cityAirport>rn </arrivalLocationInfo>rn
</availabilityProductInfo>702</orderClassesByCabin>
</cabinOption>",44,"AUHOT3116"]
i tried str_replace and trim functions in php but not working.I want out put from this string like below.
300518-departuredate-availabilityDetails-HYD-cityAirport-departureLocationInfo-DXB-
cityAirport-arrivalLocationInfo-availabilityProductInfo-702-
orderClassesByCabin-44-AUHOT3116
simply remove all ,r,n,t from this string. Can any one will help me.I was tried not getting result.Thanks advance..
php
closed as off-topic by Paul, Rob, Billal Begueradj, lucascaro, Devon_C_Miller Nov 12 '18 at 5:18
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Paul, Rob, Billal Begueradj
5
Possible duplicate of Remove excess whitespace from within a string
– Stephen Lake
Nov 11 '18 at 14:27
add a comment |
I Have a string data coming from for loop, i want to remove specific characters and special characters from a string.Please find below my data.
["rn rn 300518</departureDate>rnttttttttt
rnttttttttt rn </availabilityDetails>rn
rn HYD</cityAirport>rn </departureLocationInfo>rn
rn DXB</cityAirport>rn </arrivalLocationInfo>rn
</availabilityProductInfo>702</orderClassesByCabin>
</cabinOption>",44,"AUHOT3116"]
i tried str_replace and trim functions in php but not working.I want out put from this string like below.
300518-departuredate-availabilityDetails-HYD-cityAirport-departureLocationInfo-DXB-
cityAirport-arrivalLocationInfo-availabilityProductInfo-702-
orderClassesByCabin-44-AUHOT3116
simply remove all ,r,n,t from this string. Can any one will help me.I was tried not getting result.Thanks advance..
php
I Have a string data coming from for loop, i want to remove specific characters and special characters from a string.Please find below my data.
["rn rn 300518</departureDate>rnttttttttt
rnttttttttt rn </availabilityDetails>rn
rn HYD</cityAirport>rn </departureLocationInfo>rn
rn DXB</cityAirport>rn </arrivalLocationInfo>rn
</availabilityProductInfo>702</orderClassesByCabin>
</cabinOption>",44,"AUHOT3116"]
i tried str_replace and trim functions in php but not working.I want out put from this string like below.
300518-departuredate-availabilityDetails-HYD-cityAirport-departureLocationInfo-DXB-
cityAirport-arrivalLocationInfo-availabilityProductInfo-702-
orderClassesByCabin-44-AUHOT3116
simply remove all ,r,n,t from this string. Can any one will help me.I was tried not getting result.Thanks advance..
php
php
edited Nov 11 '18 at 14:23
Daniel Larsson
5372419
5372419
asked Nov 11 '18 at 14:07
NiruNiru
14
14
closed as off-topic by Paul, Rob, Billal Begueradj, lucascaro, Devon_C_Miller Nov 12 '18 at 5:18
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Paul, Rob, Billal Begueradj
closed as off-topic by Paul, Rob, Billal Begueradj, lucascaro, Devon_C_Miller Nov 12 '18 at 5:18
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Paul, Rob, Billal Begueradj
5
Possible duplicate of Remove excess whitespace from within a string
– Stephen Lake
Nov 11 '18 at 14:27
add a comment |
5
Possible duplicate of Remove excess whitespace from within a string
– Stephen Lake
Nov 11 '18 at 14:27
5
5
Possible duplicate of Remove excess whitespace from within a string
– Stephen Lake
Nov 11 '18 at 14:27
Possible duplicate of Remove excess whitespace from within a string
– Stephen Lake
Nov 11 '18 at 14:27
add a comment |
2 Answers
2
active
oldest
votes
$cleanString = preg_replace('/s+/', ' ', $yourString);
add a comment |
Please have a look at the code below:
<?php
//the data seems to be actually an array
$array = ["rn rn 300518</departureDate>rnttttttttt rnttttttttt rn </availabilityDetails>rn rn HYD</cityAirport>rn </departureLocationInfo>rn rn DXB</cityAirport>rn </arrivalLocationInfo>rn </availabilityProductInfo>702</orderClassesByCabin></cabinOption>",44,"AUHOT3116"];
//should yield
$desired_output = "300518-departuredate-availabilityDetails-HYD-cityAirport-departureLocationInfo-DXB-cityAirport-arrivalLocationInfo-availabilityProductInfo-702-orderClassesByCabin-44-AUHOT3116";
$trim = ["t","r","n","</",">"," "]; //characters or substrings you want removed
foreach($array as &$value)
$value = str_replace($trim,"-",$value); //removing unwanted characters
$value = preg_replace('/[- ]2,/','-',$value); //removing duplicates
$value = trim($value,"- "); //removing trailing and ending unwanted characters
$replaced_output = implode("-",$array); //converting the initial array to string
echo "<pre>";
var_dump($desired_output,$replaced_output);
https://3v4l.org/3cdqO
Note that the expected string and the processed string are not identical because there was an element called 'cabinOption' that doesn't fit the removing criteria, so it was left as it was.
EDIT: To have the processed string identical with the desired string, just add whatever string you want removed in the $trim
array.
https://3v4l.org/uujfC
This may not be the best way to do it, it's just a possible solution for this specific case. You should consider a more generic approach, regular expressions etc.
its working for me thank you so much.....
– Niru
Nov 12 '18 at 6:15
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$cleanString = preg_replace('/s+/', ' ', $yourString);
add a comment |
$cleanString = preg_replace('/s+/', ' ', $yourString);
add a comment |
$cleanString = preg_replace('/s+/', ' ', $yourString);
$cleanString = preg_replace('/s+/', ' ', $yourString);
answered Nov 11 '18 at 14:24
Stephen LakeStephen Lake
1,08811223
1,08811223
add a comment |
add a comment |
Please have a look at the code below:
<?php
//the data seems to be actually an array
$array = ["rn rn 300518</departureDate>rnttttttttt rnttttttttt rn </availabilityDetails>rn rn HYD</cityAirport>rn </departureLocationInfo>rn rn DXB</cityAirport>rn </arrivalLocationInfo>rn </availabilityProductInfo>702</orderClassesByCabin></cabinOption>",44,"AUHOT3116"];
//should yield
$desired_output = "300518-departuredate-availabilityDetails-HYD-cityAirport-departureLocationInfo-DXB-cityAirport-arrivalLocationInfo-availabilityProductInfo-702-orderClassesByCabin-44-AUHOT3116";
$trim = ["t","r","n","</",">"," "]; //characters or substrings you want removed
foreach($array as &$value)
$value = str_replace($trim,"-",$value); //removing unwanted characters
$value = preg_replace('/[- ]2,/','-',$value); //removing duplicates
$value = trim($value,"- "); //removing trailing and ending unwanted characters
$replaced_output = implode("-",$array); //converting the initial array to string
echo "<pre>";
var_dump($desired_output,$replaced_output);
https://3v4l.org/3cdqO
Note that the expected string and the processed string are not identical because there was an element called 'cabinOption' that doesn't fit the removing criteria, so it was left as it was.
EDIT: To have the processed string identical with the desired string, just add whatever string you want removed in the $trim
array.
https://3v4l.org/uujfC
This may not be the best way to do it, it's just a possible solution for this specific case. You should consider a more generic approach, regular expressions etc.
its working for me thank you so much.....
– Niru
Nov 12 '18 at 6:15
add a comment |
Please have a look at the code below:
<?php
//the data seems to be actually an array
$array = ["rn rn 300518</departureDate>rnttttttttt rnttttttttt rn </availabilityDetails>rn rn HYD</cityAirport>rn </departureLocationInfo>rn rn DXB</cityAirport>rn </arrivalLocationInfo>rn </availabilityProductInfo>702</orderClassesByCabin></cabinOption>",44,"AUHOT3116"];
//should yield
$desired_output = "300518-departuredate-availabilityDetails-HYD-cityAirport-departureLocationInfo-DXB-cityAirport-arrivalLocationInfo-availabilityProductInfo-702-orderClassesByCabin-44-AUHOT3116";
$trim = ["t","r","n","</",">"," "]; //characters or substrings you want removed
foreach($array as &$value)
$value = str_replace($trim,"-",$value); //removing unwanted characters
$value = preg_replace('/[- ]2,/','-',$value); //removing duplicates
$value = trim($value,"- "); //removing trailing and ending unwanted characters
$replaced_output = implode("-",$array); //converting the initial array to string
echo "<pre>";
var_dump($desired_output,$replaced_output);
https://3v4l.org/3cdqO
Note that the expected string and the processed string are not identical because there was an element called 'cabinOption' that doesn't fit the removing criteria, so it was left as it was.
EDIT: To have the processed string identical with the desired string, just add whatever string you want removed in the $trim
array.
https://3v4l.org/uujfC
This may not be the best way to do it, it's just a possible solution for this specific case. You should consider a more generic approach, regular expressions etc.
its working for me thank you so much.....
– Niru
Nov 12 '18 at 6:15
add a comment |
Please have a look at the code below:
<?php
//the data seems to be actually an array
$array = ["rn rn 300518</departureDate>rnttttttttt rnttttttttt rn </availabilityDetails>rn rn HYD</cityAirport>rn </departureLocationInfo>rn rn DXB</cityAirport>rn </arrivalLocationInfo>rn </availabilityProductInfo>702</orderClassesByCabin></cabinOption>",44,"AUHOT3116"];
//should yield
$desired_output = "300518-departuredate-availabilityDetails-HYD-cityAirport-departureLocationInfo-DXB-cityAirport-arrivalLocationInfo-availabilityProductInfo-702-orderClassesByCabin-44-AUHOT3116";
$trim = ["t","r","n","</",">"," "]; //characters or substrings you want removed
foreach($array as &$value)
$value = str_replace($trim,"-",$value); //removing unwanted characters
$value = preg_replace('/[- ]2,/','-',$value); //removing duplicates
$value = trim($value,"- "); //removing trailing and ending unwanted characters
$replaced_output = implode("-",$array); //converting the initial array to string
echo "<pre>";
var_dump($desired_output,$replaced_output);
https://3v4l.org/3cdqO
Note that the expected string and the processed string are not identical because there was an element called 'cabinOption' that doesn't fit the removing criteria, so it was left as it was.
EDIT: To have the processed string identical with the desired string, just add whatever string you want removed in the $trim
array.
https://3v4l.org/uujfC
This may not be the best way to do it, it's just a possible solution for this specific case. You should consider a more generic approach, regular expressions etc.
Please have a look at the code below:
<?php
//the data seems to be actually an array
$array = ["rn rn 300518</departureDate>rnttttttttt rnttttttttt rn </availabilityDetails>rn rn HYD</cityAirport>rn </departureLocationInfo>rn rn DXB</cityAirport>rn </arrivalLocationInfo>rn </availabilityProductInfo>702</orderClassesByCabin></cabinOption>",44,"AUHOT3116"];
//should yield
$desired_output = "300518-departuredate-availabilityDetails-HYD-cityAirport-departureLocationInfo-DXB-cityAirport-arrivalLocationInfo-availabilityProductInfo-702-orderClassesByCabin-44-AUHOT3116";
$trim = ["t","r","n","</",">"," "]; //characters or substrings you want removed
foreach($array as &$value)
$value = str_replace($trim,"-",$value); //removing unwanted characters
$value = preg_replace('/[- ]2,/','-',$value); //removing duplicates
$value = trim($value,"- "); //removing trailing and ending unwanted characters
$replaced_output = implode("-",$array); //converting the initial array to string
echo "<pre>";
var_dump($desired_output,$replaced_output);
https://3v4l.org/3cdqO
Note that the expected string and the processed string are not identical because there was an element called 'cabinOption' that doesn't fit the removing criteria, so it was left as it was.
EDIT: To have the processed string identical with the desired string, just add whatever string you want removed in the $trim
array.
https://3v4l.org/uujfC
This may not be the best way to do it, it's just a possible solution for this specific case. You should consider a more generic approach, regular expressions etc.
edited Nov 11 '18 at 14:53
answered Nov 11 '18 at 14:34
Dan D.Dan D.
580213
580213
its working for me thank you so much.....
– Niru
Nov 12 '18 at 6:15
add a comment |
its working for me thank you so much.....
– Niru
Nov 12 '18 at 6:15
its working for me thank you so much.....
– Niru
Nov 12 '18 at 6:15
its working for me thank you so much.....
– Niru
Nov 12 '18 at 6:15
add a comment |
5
Possible duplicate of Remove excess whitespace from within a string
– Stephen Lake
Nov 11 '18 at 14:27