Get String between to string preg_match [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i have this preg_match in php to get the url string between to string. but my problem is i can't get any data out of it
preg_match( "/\[(gmap)\](.*?)\[\/(gmap)\]/si", $content, $url)
i need to get the string inside of this string
[gmap]http://maps.google.com/maps/api/staticmap?zoom=15&size=325x125&maptype=roadmap&markers=color:purple|40.718217,-73.998284&sensor=false[/gmap]
but i can't get a result. why?
$url[2]
i need to have
http://maps.google.com/maps/api/staticmap?zoom=15&size=325x125&maptype=roadmap&markers=color:purple|40.718217,-73.998284&sensor=false

This works perfectly fine :
<?php
$url="";
$content = '[gmap]http://maps.google.com/maps/api/staticmap?zoom=15&size=325x125&maptype=roadmap&markers=color:purple|40.718217,-73.998284&sensor=false[/gmap]';
preg_match( "/\[(gmap)\](.*?)\[\/(gmap)\]/si", $content, $url);
print $url[2];
?>
Cheers!

It's possible that you got an issue because the .* also includes the [ character.
You could use this instead :
preg_match("/\[(gmap)\]([^\[]*)?\[\/(gmap)\]/si", $content, $url);

Related

PHP String Replace for BBCode [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I want to make a custom BBCode for my forum site, but I've run into an issue, and I'm having a hard time fixing it.
This is what's in the database for the body of the thread "[b]Bold[/b][i]Italic[/i][strike]Strike[/strike]".
However the output is displayed like this "[i]Italic[/i][strike]Strike[/strike]".
So, I'm guessing it's an issue with echoing it out, but i'm not sure how to fix it. Here's the current code:
function bbcode($input) {
$input = strip_tags($input);
$input = htmlentities($input);
$search = array('/\[b\](.*?)\[\/b\]/is');
$replace = array('<b>$body</b>');
return preg_replace($search, $preg_replace, $input);
}
while($row = mysql_fetch_array($threadquery, MYSQL_ASSOC)) {
$body = str_replace("\n",'<br>', $row['body']);
}
echo bbcode($body);
proper code should be:
$replace = array('<b>$1</b>');
return preg_replace($search, $replace, $input);

Pass variable into url parameter php [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to pass a variable from a form to another php file to create an xml file.
<?php
$sentxt = $_POST['text']; //this comes from the form php
$params = array(
'answer_url' => 'to_xml.php?msg="$sentxt"',
);
$response = $p->make_xml($params);
echo "$sentxt";
?>
Whenever I try to run this I run into a problem
The xml file keeps outputting "$sentxt" instead of the string passed to the $sentxt via the php form post.
The echo "$sentxt"; displays the right string has been passed through properly, but the string is never passed into the array.
You should replace ' with " for string interpolation.
<?php
$sentxt = $_POST['text']; //this comes from the form php
$params = array(
'answer_url' => "to_xml.php?msg=$sentxt",
);
$response = $p->make_xml($params);
echo $sentxt;
?>

json decode from "{" and "[" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
Im trying to json_decode the following:
"main":{
"temp":9.04,
"temp_min":9.04,
"temp_max":9.04,
"pressure":938.13,
"sea_level":1037.57,
"grnd_level":938.13,
"humidity":87
},
"weather":[
{
"id":801,
"main":"Clouds",
"description":"few clouds",
"icon":"02d"
}
],
$result = json_decode($json);
$temp = $result->main->temp; //is displaying the data
however
$id = $result->weather->id; //is not displaying anything
all i can see is difference that te second one have an extra "[]"
can you help me telling how can i get weather->id from that json, thank you
The weather element is an array: it contains a list of elements. To get what you want from that exact example you would want:
$id = $result->weather[0]->id;
You also might want to think about what you want to happen if there is more than one element in that array, or if there are zero.

simplexml_load_file() with a variable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I'm currently having an issue with simplexml_load_file(); my xml path is a url, that is rendered as a variable
$xurl = "domain/pathto/myfile.xml"; // This is actually a variable that returns the entire URL to where my xml file is -- this will change from file to file
$xmlpath = parse_url($xurl, PHP_URL_PATH); // to get the path of my xml file ex. /pathto/myfile.xml
$xmlpath = mb_substr($xmlpath, 1); // returns pathto/myfile.xml
here is where my problem is, when I put it into :
simplexml_load_file($xmlpath);
In my function, I get nothing appearing from the XML file
However, in my same function if I change it to
simplexml_load_file("pathto/myfile.xml");
My function works fine.
I did an echo on $xmlpath and it returns the pathto/myfile.xml just fine.
<?php echo $xmlpath; ?> // returns pathto/myfile.xml
What am I doing wrong?
EDIT: Phil
echo strcmp("pathto/myfile.xml", $xmlpath)
returns a 0.

What is wrong with strpos()? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
Have code:
$url = 'http://www.domain.com/dir1/dir2/dir3/page-2.html';
if ($strpos = strpos('/page', $url)) echo '1';
else echo '2';
It shows only '2'.
How can I fix it?
Change arguments places
strpos($url, '/page')
strpos
Actually the syntax for function was mismatched I think.
use this
$stropos = stripos($url,"/page");
echo $strpos;
Please add === identical compare in if condition and correct the position of strpos parameters
$url = 'http://www.domain.com/dir1/dir2/dir3/page-2.html';
if ($strpos == strpos($url, '/page')) echo '1';
else echo '2';

Categories