Sorry for a silly question,
but I have a variable (say $my_data) in which data is stored, where many times URL like href="http://PAGE_PATH/the_page_name" has been used.
I want that all href="http://PAGE_PATH/the_page_name" where the_page_name varies every single time gets replaced with onclick="jsonData('the_page_name', 'something')"
But I am stuck as the_page_name are different every time.
I think some str_replace like functions may be used? I don't know.
Help Appreciated!
EDIT
I have an example that I used previously but here the_page_name did not matter:
$base_paths = array("http://PAGE_PATH");
$web_paths = array(link_url()."page");
$content = str_replace($base_paths,$web_paths, $this->input->post('pg_content'));
I think a regex is your solution
a link here
Explanation
/href="https?:/{2}\S*/([^\r\n\t\f ]+)"/g
href="http matches the characters href="http literally (case sensitive)
s? matches the character s literally (case sensitive)
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
: matches the character : literally (case sensitive)
/{2} matches the character / literally (case sensitive)
{2} Quantifier — Matches exactly 2 times
\S* matches any non-whitespace character
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
/ matches the character / literally (case sensitive)
1st Capturing Group ([^\r\n\t\f ]+)
Match a single character not present in the list below [^\r\n\t\f ]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\ matches the character \ literally (case sensitive)
r matches the character r literally (case sensitive)
\n matches a line-feed (newline) character (ASCII 10)
\t matches a tab character (ASCII 9)
\f matches a form-feed character (ASCII 12)
** ** matches the character [space] literally (case sensitive)
" matches the character " literally (case sensitive)
<?php
$mydata = 'Sorry for a silly question, but I have a variable (say $my_data) in
which data is stored, where many times URL like
href="http://PAGE_PATH/the_page_name1" has been used.
I want that all href="http://PAGE_PATH/the_page_name2" where the_page_name2
varies every single time gets replaced with onclick="jsonData(\'the_page_name\', \'something\')"
href="http://PAGE_PATH/the_diff_name"
href="http://PAGE_PATH/the_other_name"
But I am stuck as the_page_name href="http://PAGE_PATH/another_name" are different every time.
I think some str_replace like functions may be used? I dont know.
Help Appreciated!';
$pattern = '/href="https?\:\/{2}\S*\/([^\\r\n\t\f ]+)"/i';
$replacement ='onclick="jsonData(\'\1\',\'something\')"';
echo "BEFORE<br/>$mydata<hr/>AFTER<br/>";
echo preg_replace($pattern, $replacement, $mydata );
?>
Related
I would like to use a regular expression that finds only functions that are empty in php files
For example
function name_not_important()
{
}
Regex can be function\s[^\(]+\([^)]*\)(\n)*{(\n)*}
From https://regex101.com/:
function matches the characters function literally (case sensitive) \s matches any whitespace character (equivalent to [\r\n\t\f\v ])
Match a single character not present in the list below [^(]
matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy) ( matches the
character ( literally (case sensitive) ( matches the character (
literally (case sensitive) Match a single character not present in the
list below [^)]
matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy) ) matches the
character ) literally (case sensitive) ) matches the character )
literally (case sensitive) 1st Capturing Group (\n)*
matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy) A repeated capturing
group will only capture the last iteration. Put a capturing group
around the repeated group to capture all iterations or use a
non-capturing group instead if you're not interested in the data \n
matches a line-feed (newline) character (ASCII 10) { matches the
character { literally (case sensitive) 2nd Capturing Group (\n)*
matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy) A repeated capturing
group will only capture the last iteration. Put a capturing group
around the repeated group to capture all iterations or use a
non-capturing group instead if you're not interested in the data \n
matches a line-feed (newline) character (ASCII 10) } matches the
character } literally (case sensitive) Global pattern flags g
modifier: global. All matches (don't return after first match) m
modifier: multi line. Causes ^ and $ to match the begin/end of each
line (not only begin/end of string)
Note: This regex assumes that indentation of braces are in alignment.
I need validate url. I need allow only main url sites, example:
http://example.com
https://example.com
I need prevent these urls on my site:
http://example.com/page/blahblahblah
https://example.com/other/bloa
I use regex:
'url' => ['required', 'url', 'regex:/((http:|https:)\/\/)[^\/]+/']
When user insert url, he can insert http://example.com/page/blahblahblah why? My regex is not working.. Validation is passing
You can use the following pattern to ensure a URL does not contain subdirectories:
^(?:\S+:\/\/)?[^\/]+\/?$
Explanation:
^ asserts position at start of the string
Non-capturing group (?:\S+://)?
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
\S+ matches any non-whitespace character (equal to [^\r\n\t\f\v ])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
: matches the character : literally (case sensitive)
/ matches the character / literally (case sensitive)
/ matches the character / literally (case sensitive)
Match a single character not present in the list below [^/]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
/ matches the character / literally (case sensitive)
/? matches the character / literally (case sensitive)
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
You could write a custom validator and use a combination of filter_var and parse_url?
Something as follows will do the job...
<?php
$url = "http://example.com/page/blahblahblah";
if (!filter_var($url, FILTER_VALIDATE_URL)) {
return false;
}
$parts = parse_url($url);
echo "{$parts['scheme']}://{$parts['host']}";
When I replace spaces, dots and commas of a string, it sometimes happens that I get double hyphens.
For example turns check out the 1. place into check-out-the-1--place
How can I avoid that? I want it to be check-out-the-1-place - so that there only is one hyphen between each word.
Here is my code:
str_replace([' ', ',', '.','?'], '-', strtolower($pathname));
Right now, I know why it returns the double-hyphens, but I don't know how to work around that.
Can someone help me out?
How can I avoid that? I want it to be check-out-the-1-place - so that there only is one hyphen between each word. Here is my code:
Whilst Mohammad's answer is nearly there, here is a more fully working PCRE regex method and explanation as to how it works, so you can use it as you need:
$str = trim(strtolower($pathname));
$newStr = preg_replace('/[\s.,-]+/', '-', $str);
How this works:
Match a single character present in the list below [\s.,-]+
+ Quantifier Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\s matches any whitespace character (equal to [\r\n\t\f\v])
.,- matches a single character in the list .,- (case sensitive)
The dash - must come at the end of the [] set.
Results:
This: check out the 1. place
Becomes:
check-out-the-1-place
And
This: check out the - 1. place
Becomes
check-out-the-1-place
Further:
I would go further and assuming you are using this for a URL slug (a what?!); strip out all non-alphanumeric characters from the string and replace with a single - as per typical website slugs.
$newStr = preg_replace('/[^a-z0-9]+/i', '-', $str);
How this works:
Match a single character NOT (^) present in the list below [a-z0-9]+
+ Quantifier Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
a-z a single character in the range between a (index 97) and z (index 122) (case sensitive)
0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
The i at the end indicates the judgements are case In-sensitive.
Example:
check out - the no.! 1. Place
Becomes:
check-out-the-1-Place
You can use preg_replace() instead and user regex to selecting multiple specific character.
$newStr = preg_replace("/[\s.,]+/", "-", $str)
Check result in demo
I have this string that I want to clean up using PHP and regex:
Name/__text,Password/__text,Profile/__text,Locale/__text,UserType/__text,Passwor
dUpdateDate/__text,Columns/0/Name/__text,Columns/0/Label/__text,Columns/0/Order/
__text,Columns/1/Name/__text,Columns/1/Label/__text,Columns/1/Order/__text,Colum
ns/2/Name/__text,Columns/2/Label/__text,Columns/2/Order/__text,Columns/3/Name/__
text,Columns/3/Label/__text,Columns/3/Order/__text,Columns/4/Name/__text,Columns
/4/Label/__text,Columns/4/Order/__text,Columns/5/Name/__text,Columns/5/Label/__t
ext,Columns/5/Order/__text,Columns/6/Name/__text,Columns/6/Label/__text,Columns/
6/Order/__text,Columns/7/Name/__text,Columns/7/Label/__text,Columns/7/Order/__te
xt,Columns/8/Name/__text,Columns/8/Label/__text,Columns/8/Order/__text,Columns/9
/Name/__text,Columns/9/Label/__text,Columns/9/Order/__text,Columns/10/Name/__tex
t,Columns/10/Label/__text,Columns/10/Order/__text,Columns/11/Name/__text,Columns
/11/Label/__text,Columns/11/Order/__text,Columns/12/Name/__text,Columns/12/Label
/__text,Columns/12/Order/__text,Columns/13/Name/__text,Columns/13/Label/__text,C
olumns/13/Order/__text,MailAddress/__text,Description/__text,Columns/14/Name/__t
ext,Columns/14/Label/__text,Columns/14/Order/__text,Columns/15/Name/__text,Colum
ns/15/Label/__text,Columns/15/Order/__text
I want it to be Password,Profile,Locale,UserType,PasswordUpdateDate,Name,Label,Order...
I'm removing the /text or /__text after the word, but there are only sometimes things like Columns/0/ before the word to remove.
I tried this (below) regular expression in the regex tester, but it misses the first few items that don't have the Columns/2/ type of thing before it. I can't use a regex that will grab what's before /__text, because the / before the word is optional, like for the first Name. Any ideas how to do this? It's tough to search for this pattern or info on how to create it. Any help would be great!
[A-Za-z\/0-9]+\/([A-Za-z]+)\/[__text]
Probably easier to just match what you want and then join them on commas. Match a word (\w+) followed by \__text:
preg_match_all('#(\w+)/__text#', $string, $matches);
$result = implode(',', $matches[1]);
You could also use ([A-Za-z0-9]+) and add anything else instead of (\w+) in case it could be First_Name, First-Name, Firstname0 etc...
Regex:
(\w+)\/__text(?:(,)(?:Columns\/\d+\/)*)*
Demo
Explanation:
/(\w+)\/__text(?:(,)(?:Columns\/\d+\/)*)*/g
1st Capturing Group (\w+)
\w+ matches any word character (equal to [a-zA-Z0-9_])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\/ matches the character / literally (case sensitive)
__text matches the characters __text literally (case sensitive)
Non-capturing group (?:(,)(?:Columns\/\d+\/)*)*
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Capturing Group (,)
, matches the character , literally (case sensitive)
Non-capturing group (?:Columns\/\d+\/)*
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Columns matches the characters Columns literally (case sensitive)
\/ matches the character / literally (case sensitive)
\d+ matches a digit (equal to [0-9])
\/ matches the character / literally (case sensitive)
This is my data
ZP-S,ZB-MA-S,ZB,ZB-MA-M,ZB-MA-B,ZP,PY,ZB-ME-S,ZB-ME-M,PY,ZB-ME-B,PY-S,PY-M,PY-B,ZP-B,ZB-MA-S-LS-MLE-PES
This is my regex, which I tried
(^|,)(ZB)-?[^,]+(,|$)
my intention here is to check whether within comma separated list has word which starts with (ZB) can contain ( hypen and [A-Za-z0-9_]+) recursively
Expected match are as follows
ZB-MA-S
ZB
ZB-MA-M
ZB-MA-B
ZB-ME-S
ZB-ME-M
ZB-ME-B
ZB-MA-S-LS-MLE-PES
This should do it
(ZB[\w\-]*)
Demo here
https://regex101.com/r/D1dRxd/3
ZB matches the characters ZB literally (case sensitive)
\w matches any word character (equal to [a-zA-Z0-9_])
\- matches the character - literally (case sensitive)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed
Demo
ZB[^,]*(?=,?)
What I am looking for is to match any comma separated value that starts with the searched string ZB and keep matching as long as I don't hit a ,.
Hope this helps
Try ZB[a-zA-Z0-9\-\_]*
This will match all alphanumeric characters, underscore, and hyphen following ZB