Formatting a string in PHP? [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm going through sprintf() and other string formatting functions, but I have been unable to find an exact solution for stripping certain types of characters from a string. I wrote a function for this purpose (which seems quite nasty and not at all worth sharing here) but I am sure there is a easier way for what I am looking for.
$var = "abc244$%!";
now I want to format it this way:
$alpha = some_function($var); // alphabets only
$num = some_function($var); // numbers only
$alpha2 = some_function($var); // alphabets and special characters, no numbers.

To strip everything except numbers, use this:
$allnums=preg_replace("/[^0-9]/","",$var);
For all letters:
$letters=preg_replace('/\PL/u', "", $var);
For special chars:
$specialchars=preg_replace("/[a-zA-Z0-9]/", "", $var);

Related

Remove characters from a string till another character comes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do I remove characters till there comes another character in a string?
For example:
String is this: 0030051
Now I want to remove ALL 0's before another character (like: 1) comes.
So the string will become this: 30051.
Is there a PHP function for this or is this easier with Javascript/jQuery ? (I'd prefer PHP)
cast it to integer
$var = (int)$var;
Source - Reference

php query where a word like and skip before words [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
php script query out put is
"some word STAR some other words", I want remove all workd before "Star". I need all other words after "Star".
I using php script output from a mysql file
$newStr = substr_replace($str, '', 0, strpos($str,"STAR"));
Where $str is a variable with a string, eg: "some word STAR some other words"
$arrayStr = explode('STAR', 'some word STAR some other words');
echo $arrayStr[1];
DEMO
Some documentation:
php.net explode() function

PHP Individual Character validation. Regex? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Not terribly familiar with regex, but I'm guessing it's going to make life much easier for my current need.
I need to validate that a string contains the correct sequence of numbers and letters, so that it always follows the format "AA99999A", where A can be any A-Z character and 9 can be any 0-9 character. Other than exploding the string and validating the characters individually, how would the best way to handle this be?
The result can be a simple true / false as I don't need to specify which characters are incorrect
How about:
preg_match('/^[A-Z]{2}\d{5}[A-Z]$/', $string);
You can try this regex:
'/^[A-Z]{2}\d{5}[A-Z]$/i'
PHP's preg_match will do the trick:
$string_to_be_validated = "AB12345C";
if (preg_match("/\A[A-Z]{2}[0-9]{5}[A-Z]{1}\z/", $string_to_be_validated)) echo "valid";
else echo "invalid";
Here you can find more information on this.

Merge hyphenated word from string in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Suppose a string contains "includ- ing" or "joi- ing". How would you search for and join hyphenated words in a string?
You could use a regular expression with preg_replace():
preg_replace('/-\s+/', '', $string);
This looks for a - followed by one or more whitespace characters and replaces them with the empty string.
// -- Get an array of words splitted with '- '
$splitted = explode('- ', $word);
// -- Rejoined words:
$joined = implode('', $splitted );

Assistance with PHP Regex [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I don't usually use regular expressions, hence my question. I need a regex to match the following:
'{any-string}'.
Any assistance appreciated.
The most simple expression would be:
/{(.*?)}/
If you expect more complex strings (for example, some kind of escape sequence where the { and } characters are allowed within the string) it could be more complex. For instance, with a \ (backslash) escape sequence:
/{((?:\\.|[^}])*)}/
Edit: That's not tested, but the general idea is that the expression will swallow any character following the escape rather than ensuring it isn't the closing brace.
To replace anything in { } with what file_get_contents returns you can do:
$page = file_get_contents('some_file_name or some_url');
$str = '.....{...}....';
$str = preg_replace('/{[^}]*}/',"$page",$str);

Categories