REGEX with PHP (what am I doing wrong?) [duplicate] - php

This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 3 years ago.
Let's say I want to place the bold content into a variable called $question_text.
1} What color is the sky:
A) Answer A
B) Answer B
C) Answer C
Answer: A
How do I use preg_match to do that?
I tried:
if(preg_match("#}(.*)#", $question, $question_match))
{
//Extract the question text
$question_text = trim($question_match[1]);
But it only gives me the first line: **What color is the sky: **
What am I doing wrong?

#(?s)}(.*)+?Answer: [A-Z]#
Worked for me!

Related

Getting second last parameter from querystring with PHP [duplicate]

This question already has answers here:
Getting parts of a URL (Regex)
(30 answers)
Closed 3 years ago.
I have a URL like so:
http://example.com/var1/var2
What I'd like to is get VAR 1 from the querystring. I've tried using a regex but I'm not really very familiar with them. Is there an easier way?
Another approach
<?php
$e = explode("/",parse_url("http://example.com/a/b")["path"])[1];
// $e now is "a"

How to put specific string in into array? [duplicate]

This question already has answers here:
PHP: Best way to extract text within parenthesis?
(8 answers)
Closed 3 years ago.
I have a string like this:
[Rise] and rise [again] until lambs become [lions].
I want to get all the string inside the [] (Rise, again, lions) and put it in to an array. How can I do this?
Edit: Thank you guys, I already found the solution here: PHP: Best way to extract text within parenthesis?
You can do it with regular expressions:
$s = '[Rise] and rise [again] until lambs become [lions].';
preg_match_all('/\[([^\]]+)\]/', $s, $m);
print_r($m[1]);

replace particular value from url [duplicate]

This question already has answers here:
PHP, Delete parts of a URL variable [duplicate]
(3 answers)
Closed 4 years ago.
I want to replace '/50' with URL.my URL is
http://localhost/CI/admin/pcustomer/cprofile/50?customer=18267&mobile=&nearby=
I want url as
http://localhost/CI/admin/pcustomer/cprofile?customer=18267&mobile=&nearby=
From that little info in your question, this seems to be the simplest way to achieve "I want to replace '/50' with URL"
$url = str_replace('/50', '', 'http://localhost/CI/admin/pcustomer/cprofile/50?customer=18267&mobile=&nearby=');

PHP Replace text in string [duplicate]

This question already has answers here:
How do I replace certain parts of my string?
(5 answers)
Closed 6 years ago.
i have a problem.
I want in php to replace all 'blablabla' with 'bla'(because i hate blablabla). Here's my code:
<?php
$string = 'Dracula always says BlaBlaBla but says he never says BlaBlaBla';
$result = answer to replace here
?>
Thanks
Use srt_replace function to do that like this :
str_replace("BlaBlaBla","bla",$string);
You can use something like this:
str_replace("BlaBlaBla","bla",$string)
Also you can find it here in the docs: http://php.net/manual/en/function.str-replace.php

How to find 3 or more of a character in a string? [duplicate]

This question already has answers here:
How do I count comma-separated values in PHP?
(5 answers)
Closed 9 years ago.
$string = "oidjdssd , odi,jdois, 3089u,, oisdjsd";
How do i find out if theres more than 3 commas in the string above in the best way?
I would suggest substr_count. You can see if the result is >3 to see if there's more than three.
echo count_chars($string)[ord(',')];
Or for PHP<5.4
$chars = count_chars($string);
echo $chars[ord(',')];
BTW: As it seems, that you are handling CSV-data, you should have a look at str_getcsv()

Categories