preg_match help, simple for most - preg-match

I'm trying to match all the URLs that end in .html and if they have ? after it's OK but if it's odd then not so
preg_match("/article-(.+?).html/i", getenv('REQUEST_URI')) <-- good
preg_match("/article-(.+?).html**?**/i", getenv('REQUEST_URI')) <-- good
basically i want to match it if it end in .html or ends in .html? and if it has a ? then anything after is ok. So basically
article-1.html <-- "true"
article-1.html? <-- "true"
article-1.html?sdfasdfas <-- "true"
article-1.html%20blah <-- "false"
Thanks!

This passed my testing:
preg_match("/article-(.+?).html(?![^?\"'])/i", getenv('REQUEST_URI'))

Related

Laravel: How to check if route contains nothing after a slash

I have a condition to check what URL it is so I can set a proper tab class to be active.
I have code
`{{ Request::is('popular') || Request::is('/') || Request::is('category/*/popular') || Request::is('category/*') ? 'nav-active' : '' }}`
The last condition is wrong, for example when the URL is http://localhost:8000/category/goodwin/new that will still be correct, which I don't want. How can I write a condition to check if there is anything after category but only after one /
P.S. I have other conditions for new and top posts which work correctly
You can use url()->current() and then do string analysis on that. E.g. with preg_match and a regex, or by simply counting the number of slashes.
Example:
preg_match("/(.*)\/category\/(.*)\/(?!popular)(.*)/", url()->current());
The regex (.*)\/category\/(.*)\/(?!popular)(.*) checks whether the URL matches .../category/*/* except where the final * is popular.
That would allow you to do:
{{ (Request::is('popular') ||
Request::is('/') ||
Request::is('category/*/popular') ||
Request::is('category/*')) &&
preg_match("/(.*)\/category\/(.*)\/(?!popular)(.*)/", url()->current()) == 0
? 'nav-active' : '' }}
I would consider moving away from the ternary operator as this is getting quite bulky. You should probably also put this logic in the controller and just pass a variable to the view.

what is the else statement means in codeigniter index.php line 196

when the following else statement will be executed? if the $system_path is invalid, of cause ,it will go to the else statement. however, in this case, the else statement seems to be meaningless.
https://github.com/bcit-ci/CodeIgniter/blob/develop/index.php line 196
if (($_temp = realpath($system_path)) !== FALSE)
{
$system_path = $_temp.DIRECTORY_SEPARATOR;
}
else
{
// Ensure there's a trailing slash
$system_path = strtr(
rtrim($system_path, '/\\'),
'/\\',
DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
).DIRECTORY_SEPARATOR;
}
The point of that if/else block is to construct a string that makes any kind of sense for use in the if block that follows. That block starts with
if( ! is_dir($system_path))
If realpath() returns FALSE then $system_path as written cannot be resolved. But that might not mean the path does not exist. Perhaps the developer knows something that 'realpath()' cannot figure out. The else part gives the developer the benefit of the doubt. It takes the supplied value of $system_path and formats it for use in is_dir($system_path).
Off the top of my head I cannot think of an example where realpath() would fail but where $system_path does exist. But developers are a creative bunch.

Chef - print log at the end of statement if source isn't valid

this is my code:
if node['app']['source']
src = "#{node['app']['source']}\\#{node['app']['file_name']}"
else
src = "second_source"
end
I want to add a log.warn at the end of my statement in case of any source isn't valid,
something like:
if node['app']['source']
src = "#{node['app']['source']}\\#{node['app']['file_name']}"
else
src = "second_source"
whatever
Chef::Log.warn "This path #{src} is not supported, check attributes again"
return
end
I will glad if somebody has an any idea,
Thank you...
That isn't how code works, a condition can either be true or false, and the two branches of if/else cover both cases. You would have to come up with a way to check if a source is valid and use if/elsif/else or similar.

Why is this lighttpd url rewrite not workng?

I have the following mod_rewrite code for lighttpd, but it does not properly foreward the user:
$SERVER["socket"] == ":3041" {
server.document-root = server_root + "/paste"
url.rewrite-once = ( "^/([^/\.]+)/?$" => "?page=paste&id=$1")
}
It should turn the url domain.com/H839jec into domain.com/index.php?page=paste&id=H839jec however it is not doing that, instead it is redirecting everything to domain.com. I dont know much about mod_rewrite and would appreciate some input on why it is doing this.
Use the following :
url.rewrite-once = ("^/(.*)$" => "/?page=paste&id=$1")
I don't know the exact issue in your code, but first the regex looks unnecessarily complicated and may not match what you expected it to match, and second you're redirecting to a query string where as I would expect you still need to redirect to a valid path before the query string, that's why I redirect to /?page... instead of just ?page....

Testing whether string starts with or end with another string

How should I check whether a string starts or ends with a given string? There doesn't seem to be any built-in methods available (or maybe it's just the IDE I'm using that isn't having it show up: RDE)
There are built in methods:
"String".start_with? "S" # true
"String".end_with? "4" # false
string.start_with?("substring")
string.end_with?("substring")
Returns true or false
Source:
http://ruby-doc.org//core-2.2.0/String.html#method-i-start_with-3F
http://ruby-doc.org//core-2.2.0/String.html#method-i-end_with-3F

Resources