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 would like a regular expression in PHP to find all HTML entities such as "<br /> <br /> ..etc. " in order to remove them from a long string.
This one removes alpha, decimal and hex HTML entities:
$text = preg_replace('/&(?:[a-z\d]+|#\d+|#x[a-f\d]+);/i', '', $text);
Try using this regex... the [^\s]* one was capturing a whole lot other nonsense...
$text = preg_replace("/&(?:[a-z0-9]{2,8}|#[0-9]{2,3}+);/i", '', $text);
Try using strip_tags function, regex is not necessary here
Related
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
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 not that good at Regex,
Can you give me a pattern to match any php tags? [<?, <?php]
if you have a nice and simple guide for Regex i'd be happy to have it too :)
I already tried:
'/^(<\?)$/'
But it doesn't really help =/
Thanks!
You will need preg_match_all to match tags, plural. The below will match blocks of PHP and store the code.
preg_match_all( '/<\?php(.+?)\?>/is', $php, $blocks );
print_r( $blocks );
One issue will be if you happen to have the string '?>' in your code not occuring as a PHP closing tag. Would need to know more about your file(s) to advise.
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.
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
Need help with regular expression for <!-- A-Za-z0-9 --> where all the uppercase, smallcase and digits are allowed. Need a regular expression to find this chunk including the comment tags (<!-- -->).
[<!\-\-A-Za-z0-9\-\->]
This is a character class and the order doesn't matter in a character class. What will work however:
<!--\s*[A-Za-z0-9]+\s*-->
If you now want to include spaces inside the comment, you can use:
<!--\s*[A-Za-z0-9 ]+\s*-->
I used the \s* in case there are several white spaces between the <!-- and --> and the part to capture.
Use preg_match_all and if you want to make the regex shorter, you can use the i flag. something like this:
preg_match_all('~<!--\s*[a-z0-9]+\s*-->~i', $page, $matches);
This is all I can think of according to your question
#<!--([A-Za-z0-9]*)-->#
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 );