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 7 years ago.
Improve this question
I need to get a substring (see examples, bold part) from the string. All strings begin with "input" followed by 2 underscores with some (1 to 7) random chars between. Thank you!
Examples:
input_7ax8_SOME_INFO
input_3f0max2_SOME_OTHER_INFO
input_k_ANOTHERINFO-any-chars-possible:0123456789
Using the detection of "non underscore" + "underscore" times 2 and fetching everything that comes after that you can get the result you ask.
The ?: is meant for not returning the result of the parts with underscores because the () are needed to combine it together.
$input = 'input_k_ANOTHERINFO-any-chars-possible:0123456789';
preg_match( '~^(?:[^_]+_){2}(.*)$~', $input, $match );
var_export($match);
You just need explode and its third param :
<?php
$input = 'input_7ax8_SOME_INFO';
$input = explode("_",$input,2); // Split 2 times
$input[2] = '<b>'.$input[2].'</b>'; // Make the rest of the string bold
$input = implode("_",$input); // re joining
echo $input;
?>
Related
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 2 years ago.
Improve this question
I need to retrive a selected part from a url . I've used substr method and i've successfully get the character. But my issue is that ,this is my sample url localhost/xxxxxxx/sugar_daddy_member-1.xml i need to retrive the last number in the url. By using this below given code i can sucessfully get the number but if two digit number comes in the url i can retrive only one number.
$page = 'sugar_daddy_member-10';
$last_char = substr($page, 19, 1);
You can use the strrpos function to find the dash.
<?php
$page = 'sugar_daddy_member-10';
$idx = strrpos($page, '-');
$last_char = substr($page, $idx+1);
echo ($last_char);
?>
Output
10
Try this one using PHP explode() Function
<?php
$page = 'sugar_daddy_member-10';
$temp = explode("-",$page);
echo $temp[count($temp)-1];
?>
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 4 years ago.
Improve this question
I can't seem to get this correct.
I have this PHP command that I need to do the same thing in Python:
preg_match("/^(collabedge-|cb|ss)([0-9]+).dc-([0-9]+).com$/", $domain, $matches);
I have three possible string formats:
domain = collabedge-123.dc-01.com
domain = cb123.dc-01.com
domain = ss123.dc-01.com
I need to pull out the 123 and 01 from the string no matter what the format of the string and assign to variables.
You can use this code:
import re
domain = "collabedge-123.dc-01.com"
# preg_match("/^(collabedge-|cb|ss)([0-9]+).dc-([0-9]+).com$/", $domain, $matches);
regex = r"^(collabedge-|cb|ss)([0-9]+).dc-([0-9]+).com$"
res = re.match(regex,domain)
print(res.group(0))
print(res.group(1))
print(res.group(2))
print(res.group(3))
Output:
collabedge-123.dc-01.com
collabedge-
123
01
As sure as I posted the question I kept trying and figured it out. For those that want to know the answer.
d = re.match(r"(collabedge-|cb|ss)([0-9]+)\.dc-([0-9]+)\.com", domain)
firstnum = d.group(2)
secnum = d.group(3)
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 5 years ago.
Improve this question
$data[10] = "12.21661,13.00128";
$loc = explode(",",$data[10]);
$lat = $loc[0];
$long = $loc[1]; **// Undefined offset:1**
When i run above code I got error on $long = $loc[1]; line (Undefined offset:1).
How can I resolve this??
The data in string is 2 digits and 5 decimals. if the number have fixed limits then there is function in PHP : str_split()
Here's documentation , Look at an example , it will be much clear.
it will split in exact length like
Check example : Example #1 Example uses of str_split() in documentation
http://php.net/manual/en/function.str-split.php
or
there is explode() function
http://php.net/manual/en/function.explode.php
does exactly what you want , I guess.
check out , examples are quite simple.
Any query comment back.
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 7 years ago.
Improve this question
I loaded a websites html onto a string and it has several g.load() code in the body - maybe around 10. I need this particular one which has an identifier of divId:"listing-provided-by-module".
g.load({ajaxURL:"/AjaxRender.htm?encparams=4~502222143586867513~LVuCHKHFeed3jCcefsa9MDj3xIs5wDqP7UwvtV3XDO0HnrynNRzT338AKMnzqNa4bTpgvQbff_Phk5wkav9LlWUqZfiIFKl3zXnXawc1_XDPR_9F83BlTaqhCqbfubm40s0ZciFJZV2dHzDDwlDVJJzitcXFgThESVdjnWUjJkj_MuZSVclGh7ddZ0neIHCH&rwebid=46328989&rhost=1",jsModule:"z-complaint-manager-async-block",phaseType:"scroll",divId:"listing-provided-by-module"});
What I need exactly is the ajaxURL and at the same time it checks that it is indeed with divId:"listing-provided-by-module" so it gets the correct url. Hope you can help with the regex and PHP for this. Thanks!
I've tried:
/g\.load\(\{ajaxURL:"(.*)".*divId:"listing-provided-by-module"/
But the match is too wide.
Here is the whole HTML body I want to match against: http://pastebin.com/seJd2jjc
Try this (visualized here):
'/g\.load\(\{ajaxURL:"([^"]*)",[^})]*,divId:"listing-provided-by-module"/s'
Your URL will be contained in the first capture group.
So, the PHP code would be:
<?php
$matches = [];
if (preg_match('/g\.load\(\{ajaxURL:"([^"]*)",[^})]*,divId:"listing-provided-by-module"/s', $string, $matches))
{
$url = $matches[1];
}
else
{
// No match found.
}
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
}