Find all reaccurances of text inbetween two strings [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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.
Closed 9 years ago.
Improve this question
I'm trying to find a text between $something and $something_else and ditch it out in an array.
I would think you need preg_match to do this but I have been trying alot and still have no idea.
This should work no matter what $something and $something_else is.

You need to read the documentation of preg_match and preg_match_all.
Here's a simple example that will match whatever content inside (double quotes)..
<?php
preg_match_all('~"(.*?)"~',
'Hey there "I will be matched", because "I am inside the double quotes"',
$out, PREG_PATTERN_ORDER);
print_r(($out[0]));
OUTPUT :
Array
(
[0] => "I will be matched"
[1] => "I am inside the double quotes"
)

Correct me if i am wrong. We can use explode one string into a array. Use pre_match_all for the another string with each word of the array . In this way, it will work with any string.

Related

How to check a string contain alphabet or not [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a simple php program which is getting the data from the url, but I want to check whether it contains a alphabet or not.
The data which I get from the url is as follows ?query=seller,12. I used ctype_alpha() for this, but I am getting no result for this. I have a rough idea that it can be done by preg_match() but I don't how to do it.
Please help me as I am beginner to php.
Thanks in advance
Here you go,
<?php
$subject = "?query=seller,12";
if(preg_match('/[a-zA-Z]/', $subject)){
echo "It has a alphabet";
}
?>
if you want to print all the characters from that string, you can use like this
preg_match_all('/[a-zA-Z]/', $subject,$matches);
print_r($matches);
$matches is an array of all available matches of the specified pattern
Try this dude.
if (!preg_match('/[^A-Za-z]/', $string)) // '/[^a-z\d]/i' should also work.
{
// string contains only english letters
}

How to recognize strings in php? [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 8 years ago.
Improve this question
When I type:
<?php
$temp="1234";
echo "<script type='text/javascript'>document.write({$temp});</script>";
?>
I get 1234 on the screen.
But when I replace $temp="1234" with $temp="alfa" I get nothing on the screen.
What's the problem ? Where am I wrong ? "1234" and "alfa" are both strings
alert(1234) is valid javascript, and will simply pop up the integer 1234 on your screen.
alert(alfa) is attempting to pop up the contents of a variable named alfa on your screen, which doesn't exist.
If you're insert data from PHP into a Javascript context, you MUST use json_encode() to ensure that you're producing VALID javascript. e.g.
$temp = 'alfa';
$json= json_encode($temp);
echo '<script>.... {$json}...</script>';
that will produce alert('alfa') and work as expected.
You aren't enclosing the text in the double-quote character '"' which denotes a string.
document.write(1234) works because 1234 is a valid value in JavaScript (an integer), whereas alfa is a symbol name, it should be "alfa".

explode function doesn't work [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have a String 7*10*6* I will separate the numbers, when I use explode function the out is:
Array
(
[0] => *
)
here is my code:
echo '<pre>';
print_r(explode('7*10*6','*'));
echo '</pre>';
what's the problem?
Oh boy, have you read the manual of explode?
the manual says:
explode(string $delimiter,string $string). You have done wrong. change your code to this:
print_r(explode('*','7*10*6'));
Try
print_r(explode('*','7*10*6'));
In explode function first argument is separator and second is string.

How to convert te JSON string to a JSON object in php? [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 got a object like this:
{"1":"FFS","2":"S"}, and I use json_decode function to convert it into an array, but I am not success with it. It becomes the array like this: {"FFS", "S"}, but missing the {"1", "2"}, Can I convert it to become a dict or something, that I can access both value? Thanks.
use true param, to convert object to associative array for json_decode(), like do:
$str = '{"1":"FFS","2":"S"}';
echo "<pre>"; print_r(json_decode($str, true));
gives::
Array
(
[1] => FFS
[2] => S
)
$myjsonobject = json_decode('{"1":"FFS","2":"S"}');
Should working.
Try: print_r($myjsonobject); to validate.
Its working.

PHP PCRE syntax [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'm a newbie with PCRE in PHP. I'm trying to make a very basic shortcode function that could make something with a format like this one: {somealphanumericthing}
In essence I need a preg_match_all() that could find in my post these type of occurrences. I tried something like this:
$shortcode = preg_match_all('/^\b\{[a-zA-Z0-9_]+(\}\b)$/', $body, $found);
var_dump($shortcode);
if($shortcode==1) {
for($i=0;$i<count($found);$i++) {
print_r($found);
//do something nice
}
}
But unfortunately it's not working: I get int 0 to the test string {test}
A few things about the regular expression:
You don't need your line anchors since you are searching in a larger string.
There's not need to capture the closing }
Optimization, use the character class \w
Condensed:
/\b\{[a-zA-Z0-9_]+\}\b/

Categories