Optional action route preg match PHP [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 8 years ago.
Improve this question
Example:
http://mysite.domain/user1 //profile user 1
http://mysite.domain/user1/photos //photos user 1
http://mysite.domain/user1/friends //friends user 1
Action of user only photos or friends.This is my code:
if (preg_match('/^\/([\w\-]+)(\/[photos|friends]+)$/i', $url, $m)) {
//code...
}
But not working, somebody can help me?

One error is [photos|friends]+. This will match too broadly, namely any combination of the characters mentioned, including |; not the whole string in the order you put it (either 'photos' or 'profile').
I think what you want for the expression is '/^\/([\w\-]+)\/(photos|friends)$/i' — that’s assuming $url is '/user1/photos' or '/user1/friends'.
I would suggest when matching URLs to use a regex delimiter other than /, because having to escape all the slashes makes the expression hard to comprehend. Personally, I like to use the comma (as it’s rather rare in URLs): ',^/([\w\-]+)/(photos|friends)$,i'.
Update
To make the last part (/photos or /friends) optional, use ',^/([\w\-]+)(/(photos|friends))?$,i'. If you want to allow /user1/ (with the trailing slash), you can keep it even simpler ',^/([\w\-]+)/(photos|friends)?$,i'.
The ? will make the preceeding token optional (in the first case, this is (/(photos|friends)), in the second case, it’s just (photos|friends).

Related

Regex that finds JSON string inside another string [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 3 years ago.
Improve this question
I would like to extract a JSON string within another string.
Currently I am getting the full string using file_get_contents and running the following pattern on the string: https://regex101.com/r/5jAucO/1
which pretty much gives multiple matches.
I would like to extract the JSON string that is saved in window._sharedData but haven't been able to achieve that. Does someone have any idea how I could do that?
Why not include _sharedData in the regex like?
_sharedData\s*=\s*\{.+\}
or with lookbehind:
(?<=_sharedData\s=)\s*\{.+\}
or take the json from a capturing group:
_sharedData\s*=\s*(\{.+\})
One concern with the lookbehind is if they add an additional whitespace character between _sharedData and = it won't match.
This only works well since there are no linebreaks in the JSON.

Find Price/Value in PHP string [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'm trying to use PHP to find a value in a string and replace it with another value, the value may be (for example) £x on one line and £x.xx on the next, but will always be replaced by a value of £x.xx. Hope that makes sense? TIA.
Use this regex to match the prices: £[0-9]+\.?[0-9]{0,2}
You can perform the replace with this by using this function:
preg_replace("/£[0-9]+\.?[0-9]{0,2}/", " ** REPLACEMENT ** ", $input_lines);
The nice thing about preg_replace is that if you feed it a single line you'll get a single value back, but if you feed it an array of strings with the values you want changed it will return an array with those values replaces. Nice and tidy if you need to make a lot of replacements.

When passing variables on url %271%27 [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
This...
header("Location: fichaTorneioFinSub.php?id=" . $_GET['id'] . "");
The value of the GET is 1. But I always end up in this page:
fichaTorneioFin.php?id=%271%27
The id value gets these %27 around it. I know that means it's an encoded single quote but it shouldn't be there.
It would be a good idea to track down what's giving you the lone ', but this will ensure you're only working with digits:
$id = preg_replace('/\D/', '', $_GET['id']);
header("Location: fichaTorneioFinSub.php?id=".$id);
This will sound as stating the obvious: if it shouldnt be there, make sure it isnt. This means sanitizing your input ($_get data) into data you expect. Somehow the get variable already contains a single quote so i would suggest checking that first. To get it out you could for example
trim($_get ['id'], "'")

Prevent special characters like space in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I have a field that users can enter whatever hey want, And I would allow them for decoration using special characters. but Now I really face with a big problem!
Special characters are like this: ♥♦☻NAME☻♦♥
And my really problem is 'alt+255' characters. it's like space and there are so many special characters like space. by the way My links are disabled and no one could select it.
There is a mandatory to enter more than only 1 character,
I want to know how to prevent this problem. my exact mean How can I let users enter special characters but still my links are clickable
If you are including the text in URLs then you really have two options. The most common approach is to strip out everything except for letters, numbers, dashes, and underscores (i.e. don't allow any special characters at all). You could use a simple regular expression replacement to do that.
Alternatively, you could allow all special characters, but escape them for use in links. You will find PHP's urlencode() and urldecode() useful for that.

Inserting some text into multiple lines 32 characters before the line ends [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
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
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Improve this question
So I need to add '0', to every line of a 22245 line file, all the values are different so find and replace isn't working, I'm wondering if there's a regex way or something that I can use notepad++ to insert thins 32 characters from the end of each line?
Or maybe a different program or way? I know a php script would allow me to insert a variable number of spaces from the beginning or the end, but it seems like an unnecessary amount of effort.
Using notepad++, you can use a capture group ( ( ... ) ), the end of line anchor ($) definite quantifier {32} to mean 32 characters, the wildcard . and the replace backreference in the replace box like this:
Find:
(.{32})$
Replace with:
0$1
Or use a positive lookahead, find:
(?=.{32}$)
Replace with:
0
Make sure you have checked the box for regular expression search.
If you want to insert a word / line at a specific line, you can use the following solution. It reads the whole file contents into an array, and use array_splice() to insert the new word into it:
// read the file into an array
$lines = file('file.txt');
// set the word and position to be inserted
$wordToBeInserted = 'foo';
$pos = 32;
// add the word into the array and write it back
array_splice($lines, $pos-1, 0, array("$wordToBeInserted\n"));
// write it back
file_put_contents('file.txt', implode('', $lines));

Categories