Remove spaces from string but not all [duplicate] - php

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 7 years ago.
To be clear, i want this:
Europe Qualicitaions
to transform to this:
Europe Qualifications
I getting data from xml feed, and some strings like this up has more than one space, problem is that i don't know how many spaces will be in string, so i want to remove all spaces before, and after string, also remove separator spaces from middle more than one, i want to have one space in middle, but no 2,3-10...

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

Related

What is the regular expression I should use in preg_replace() to find tabs inside all quoted strings and replace with a single space? [duplicate]

This question already has answers here:
How do I replace tabs with spaces within variables in PHP?
(9 answers)
Replace all occurences of char inside quotes on PHP
(5 answers)
Closed 5 years ago.
I know what needs to be done, but have been unsuccessful with the correct regex.
What is the regular expression I should use in preg_replace() to find tabs inside all quoted strings and replace with a single space?
any help would be much appreciated.
Example:
$string = '"Foo\tMan\tChoo"'
preg_replace($expression_string, ' ',$string);
echo $string;//desired result---->'"Foo Man Choo"'
I think this question is not a duplicate
answer : you will need to repeat preg_replace as many time as the max of \t you expect in a field (never could be bothered comming up with a more generic solution : it is usually possible to use this one)
also make sure every line ends with a tab (otherwise the last field will not be processed)
then you need to repeat replacement starting with the max possible number of \t (2 in the example)
$string = '"Foo'."\t".'Man'."\t".'Choo"'."\t".'boo'."\t".'"Foo'."\t".'Man"'."\t";
$string = preg_replace('/"([^"]*)'."\t".'([^"]*)'."\t".'([^"]*)"'."\t".'/','"$1_$2_$3"'."\t",$string);
$string = preg_replace('/"([^"]*)'."\t".'([^"]*)"'."\t".'/','"$1_$2"'."\t",$string);
echo $string;

How to split a very long string to multiple string on Android Studio? [duplicate]

This question already has answers here:
Split string to equal length substrings in Java
(23 answers)
Closed 6 years ago.
Hello i wanna split a very long string ( +10000 character) to multiple String.
I found there is a way with split() but i don't have space " " to split it.
I have only letter. Can i use a loop to split it?
For Example: it 's possible to split the string to multiple strings with 200 character for each one.
As solved by this link, you can use Regex to achieve your requirement
try
System.out.println(Arrays.toString(
"Thequickbrownfoxjumps".split("(?<=\\G.{4})")
));

php preg_replace: Replace word, even with special chars in it [duplicate]

This question already has answers here:
How to I remove special characters and spaces on a textfield using PHP
(3 answers)
Closed 6 years ago.
I need to find and replace a word like "test", but even if it has none-alphabetic chars in it, like:
test
t e s t
t.e.s.t
t-e-s-t
t.e-s(t
etc.
Any ideas how to do this in php? Perhaps with preg_replace?
Try this:
preg_replace("/t\W?e\W?s\W?t/", "something", $input_array);

Replace multiple commas in a string with a single comma using php [duplicate]

This question already has answers here:
Remove multiple commas regex
(3 answers)
Closed 7 years ago.
I have a string which has multiple commas at different places. i want to replace multiple occurances of comma in the string with a single comma.
My string is:
$string = "EN:25,,,BR:18,,UE:21,,,NED:24,FR:20";
Have a look at preg_replace. You want
$string = preg_replace("/,+/", ",", $string);
i guess.

In PHP, how can I split a string by whitespace, commas, and newlines at the same time [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to split a string by multiple delimiters in PHP?
What would be the most efficient way to split a string by three delimiters at the same time, specifically a single white space, newline, and commas?
Since you've tagged your questions with php I will stick to that. See preg_split
$split_strings = preg_split('/[\ \n\,]+/', $your_string);
This will keep the array clean too, in case your string is something like some, ,,string, it will still result in ['some', 'string'] instead of ['some', '', '', '', 'string'].

Categories