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/
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
Can anyone help me get the value of IFC/USD 0.00001031 from the webpage http://infinitecoin.com.
I need to grab the value after IFC/USD..
The way I thought to do it was to:
$file_string = file_get_contents('http://infinitecoin.com');
preg_match('/IFC/USD plus next 11 charecters', $file_string, $title);
$value = $title[1];
echo $title_out ;
But im not sure how to ask PHP to find IFC/USD then return the next 11 characters after that.
If I could accomplish this, my task would be solved..
Any help would be great.
Thank you
Jason
$output = array();
preg_match("/(?<=IFC\/USD\s)[\d\.]+/", 'IFC/USD 0.00001015', $output);
$output will look like this:
Array
(
[0] => 0.00001015
)
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 can find many examples of finding URL's withing HTML, however, I need to be more specific and extract the URL for the image in the following piece of text:
I will only need the first match.
I am using PHP.
Expected value result: http://cdn.somewebsite.com/path/123.jpg
var flashvars = {
'url_mode':'1',
'image':'http://cdn.somewebsite.com/path/123.jpg',
'bufferlength':'3',
'id': 'player',
'autostart': 'true'
...
};
Using preg_match with positive lookbehind assertion:
$data = <<< EOF
var flashvars = {
'url_mode':'1',
'image':'http://cdn.somewebsite.com/path/123.jpg',
'bufferlength':'3',
'id': 'player',
'autostart': 'true'
...
};
EOF;
preg_match("/(?<='image':')[^']+/", $data, $match);
echo($match[0]);
Hi this should work for you.
preg_match(/http.*?\.jpg/)
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