Get selected characters from a url [closed] - php

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];
?>

Related

Php string concatenating variables with forward slash [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 4 years ago.
Improve this question
I am trying to create a unique id by concatenating 3 variables using forward slashes(/). my code is like so
$year . "/" . $acronym . "/" . $num;
I am expecting an output of
"18/MC/1"
but the output I get is
"18\/MC\/1"
What am I doing wrong. I have already tried using stripslashes() but it doesn't do anything to the output.
You could use join function
<?php
// ...
join([$year, $acronym, $num], '/');
For more info, see: Join function documentation
I found what was wrong,
The code below outputs the correct format 18/MC/1
return response()->json([$id]);
while my previous code was
return response()->json($id);
which gave me an output of
`"18/MC/1"
There is function in php to remove backward slash from string use following function.
echo stripslashes(string);
output: 18/MC/1

How to remove specific word from php echo which is being executed using mysql query? [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 5 years ago.
Improve this question
I am modifying a PHP website, due to some condition I have to use existing MySQL query to display heading names on its relevant pages. The code is,
<h4> <?php echo $querysb['services'];?></h4>
My problem is, I am in a situation where I do not want to display the last word stored in the variable services. For example, if the services= football match, cricket match, I want to display only football and cricket, not the word match. Can I do it from
<?php echo $querysb['services'];?></h4>
this particular line?
Thanx in advance :-)
try this:
$heading = $querysb['services'];
$parts = explode(' ', $heading);
array_pop($parts);
$heading = implode(' ', $parts);
echo $heading;
Perhaps str_replace() would do the job?
In your case:
str_replace(["services=", "match"], "", trim($querysb['services']));
Its a bit messy, but just to show as an example
so just try to replace your fix text with string function
<?php
echo str_replace("match","","your title match");
?>
to show the 1st word only
$string = strstr($string, " ", true);

PHP Regex: extracting dynamic url from page body html [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 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.
}

PHP regexping the string (extracting) [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 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;
?>

i want to remove %20 from the url in php [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
This is my page url
http://myaliveidea.com/news/news/readmore/56/%20When%20Harry%20and%20Sally%20met%20New%20York%20City
but i want my page url like this
http://myaliveidea.com/news/news/readmore/59/When-Harry-and-Sally-met-New-York- City
the solution of this question is
<?php
$url = str_replace(' ','-',$sport['title']);
$url = str_replace(":",'',$url);
$url = str_replace("'",'',$url);
?>
<?php echo $sport_top['title'];?>
Use urldecode();
$url = urldecode($url);
then replace spaces(' ') by - using str_replace();

Categories