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
if(preg_match("/^[\w_.]+$/",stripslashes($_GET['key']))) {
$key = $wpdb->escape(stripslashes($_GET['key']));
}
assuming the key value is = be4e53680e6518cca701ec091258642f0740fe3d
can someone please explain me the if condition ? I`m confused on what exactly it checks for
ok thanks guys for the clarification on that.
now i`m posting one more line of code that ties with this one. if u can help me understand it.
if(preg_match("/^[\w_.]+$/",stripslashes($_GET['key']))) {
$key = $wpdb->escape(stripslashes($_GET['key']));
} else {
if(preg_match("/^[\w_.]+$/",$name)) {
$wpdb->query("some query;");
}
exit(0);
}
assuming $_GET['key'] = be4e53680e6518cca701ec091258642f0740fe3d
$name = TomJones
what i got so far is:
If $_GET['key'] is numeric then $key = stripslashes (get_key)
but when does the else kiks in ?
it looks for strings containing alphanumeric characters, underscores and dots in the key param from request, underscore is ommitable because \w handles it
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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
I have a variable $TYPE=K180M-2
I need to "extract only the part until the dash (K180M) in a new variable.
How can I do it?
You may try:
$arr=explode("-", $TYPE);
$arr[0] will give you the desired result.
<?php
$newType = substr($TYPE, 0, stripos($TYPE, '-'));
?>
Will work.
You can use explode :
<?php
$K180M = '45';
$TYPE = 'K180M-2';
$var = explode('-',$TYPE,2);
$$var[0]; // this is your new variable named $K180M
var_dump($$var[0]); // result 45
If you prefer a regular expression, use preg_replace()
// Delete the dash and anything following it:
$type_without_dash = preg_replace("/-.*/", "", $TYPE);
Simply use
$value=strstr($TYPE,'-',TRUE);
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 hope I worded the question correctly! I want to retrieve the name of a country if it exists within a string.
$bio = 'A biography about someone from France';
$countries = ['Germany', 'Spain', 'France'];
How could I check for the existence of a country in the string using the countries array?
And then, if a match is found, return it? In this example, I'd be left with the word France.
try this code:
print_r(array_intersect(explode(' ', $bio), $countries));
or
foreach ($countries as $v)
if (mb_stripos($bio, $v) !== false)
{
echo $v;
break;
}
or
echo #array_shift(array_intersect(explode(' ', $bio), $countries));
u have to use foreach on ur countries array, and inside check if strpos(bio, country) to find the position of the country word. if the position is not false it means it's there!!
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 want to remove a particular match of characters from a string.
For example i have strings;
topics-p10-new-model-cars
topics-p20-new-model-cars
topics-p30-new-model-cars
topics-p40-new-model-cars
Then i need the results as,
topics-new-model-cars
topics-new-model-cars
topics-new-model-cars
topics-new-model-cars
That means i want to remove p10-,p20-,etc..
.Those are the page numbers. It may be any number..
How can i do this..? Thanks in advance
Try this:
$result = preg_replace('/\-p\d+/', '', $string);
Note: I'm assuming that the string format does not change (I mean this [topics-p10-new-model-cars]). If my assumption is right.
Then you can do this
if (textBox1.Text.Contains("-p10-"))
{
//topics-p10-new-model-cars
String[] splited = textBox1.Text.Split(new char[] {'-'});
String rString = String.Format("{0}-{1}-{2}-{3}",
splited[0],splited[2],splited[3],splited[4]);
MessageBox.Show(rString);
}
//OR This method
if (textBox1.Text.Contains("-p10-"))
{
String result = textBox1.Text.Replace("p10-", "");
MessageBox.Show(result);
}
With 'preg-replace'::
For, example:
<?
echo preg_replace('/p[0-9]+\-/', '', 'topics-p10-new-model-cars');
?>
Follow this link:
http://www.php.net/manual/en/function.preg-replace.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 have an SMF website and i'm actually trying to get some header information which includes the title of a particular thread, the url but i've been finding it difficult to get the unique link affixed to the url using PHP.
Here's the url: http://example.com/index.php?topic=6449.msg6858
I'm actually looking for a way to extract the number 6449, I've tried to use the php GET function but it doesn't work.
$parts = explode('.', $_GET['topic']);
echo $parts[0];
// PHP 5.4+
echo explode('.', $_GET['topic'])[0];
See it in action
This would work, too
echo (int) $_GET['topic'];
See it in action
You want to use a combination of substr and strpos (to find the first occurence of a period)
$number = substr($_GET['topic'], 0, strpos($_GET['topic'], '.'));
// 6449
$arr = array();
if (preg_match("/([\\d]+)([.]{1}msg[\\d]+)/", $_GET["topic"], $arr) == 1) {
echo $arr[1];
} else {
trigger_error("not found", E_USER_ERROR);
}
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/