If /store/*/aproduct [duplicate] - php

This question already has answers here:
If url is /store/*
(3 answers)
Closed 8 years ago.
if($_SERVER['REQUEST_URI']=='/store/'.*.'/aproduct')
Is that possible? So if I type /store/something/aproduct or /store/abc123/aproduct the if statement happens? The '*' should be everything. Dosent matter if you type cow, 123, lol or youarenice

Try with:
if ( preg_match('~^/store/[^/]+/aproduct$~', $_SERVER['REQUEST_URI']) )

yes, you can use regular expressions for that. Something like this should work:
if(preg_match('#^\/store\/.+\/aproduct#', $_SERVER['REQUEST_URI'])) {
// do something
}

Related

Getting second last parameter from querystring with PHP [duplicate]

This question already has answers here:
Getting parts of a URL (Regex)
(30 answers)
Closed 3 years ago.
I have a URL like so:
http://example.com/var1/var2
What I'd like to is get VAR 1 from the querystring. I've tried using a regex but I'm not really very familiar with them. Is there an easier way?
Another approach
<?php
$e = explode("/",parse_url("http://example.com/a/b")["path"])[1];
// $e now is "a"

replace particular value from url [duplicate]

This question already has answers here:
PHP, Delete parts of a URL variable [duplicate]
(3 answers)
Closed 4 years ago.
I want to replace '/50' with URL.my URL is
http://localhost/CI/admin/pcustomer/cprofile/50?customer=18267&mobile=&nearby=
I want url as
http://localhost/CI/admin/pcustomer/cprofile?customer=18267&mobile=&nearby=
From that little info in your question, this seems to be the simplest way to achieve "I want to replace '/50' with URL"
$url = str_replace('/50', '', 'http://localhost/CI/admin/pcustomer/cprofile/50?customer=18267&mobile=&nearby=');

REGEX with PHP (what am I doing wrong?) [duplicate]

This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 3 years ago.
Let's say I want to place the bold content into a variable called $question_text.
1} What color is the sky:
A) Answer A
B) Answer B
C) Answer C
Answer: A
How do I use preg_match to do that?
I tried:
if(preg_match("#}(.*)#", $question, $question_match))
{
//Extract the question text
$question_text = trim($question_match[1]);
But it only gives me the first line: **What color is the sky: **
What am I doing wrong?
#(?s)}(.*)+?Answer: [A-Z]#
Worked for me!

PHP Replace text in string [duplicate]

This question already has answers here:
How do I replace certain parts of my string?
(5 answers)
Closed 6 years ago.
i have a problem.
I want in php to replace all 'blablabla' with 'bla'(because i hate blablabla). Here's my code:
<?php
$string = 'Dracula always says BlaBlaBla but says he never says BlaBlaBla';
$result = answer to replace here
?>
Thanks
Use srt_replace function to do that like this :
str_replace("BlaBlaBla","bla",$string);
You can use something like this:
str_replace("BlaBlaBla","bla",$string)
Also you can find it here in the docs: http://php.net/manual/en/function.str-replace.php

string has prefix PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP startsWith() and endsWith() functions
Check if variable starts with ‘http’
How make an if-statement that if $mystring has the prefix "http://" then {do something}.
I've done this in objective-c like this:
if([mynsstring hasPrefix:#"http://"])
{
//Do something...
}
I don't know how to do this in PHP.
Thanks for your help in advance.
Simplest would be using substring to compare
if (substr($mystring, 0, 7) === 'http://') {
// do something
}
Remember of course to take the exact number of characters.

Categories