Symfony ExpressionLanguage evaluate string with dashes - php

I'm trying to evaluate some strings containing dashes with the symfony ExpressionLanguage component.
Here is what I've got so far :
...
$string = 'user.chuck-norris.getId()';
$language = new ExpressionLanguage();
$evaluated = $language->evaluate($expression, $users);
...
This returns me the following error :
Variable "norris" is not valid around position 12. (Symfony\Component\ExpressionLanguage\SyntaxError)
If I change the dash "-" by an underscore "_", this works, but I have slug system which use dash and I dont wont to change it if I can avoid it.
Is there any solution?
Thanks

Like stated by Yonel, dashes are interpretated as operator.
So for this to work, I just have to replace dashes by undescores
$string = 'user.chuck-norris.getId()';
And then before making the request, replace _ by -
$value = str_replace('_', '-', $value);

Related

Create a function to find a specific word in the title

I have the following title formation on my website:
It's no use going back to yesterday, because at that time I was... Lewis Carroll
Always is: The phrase… (author).
I want to delete everything after the ellipsis (…), leaving only the sentence as the title. I thought of creating a function in php that would take the parts of the titles, throw them in an array and then I would work each part, identifying the only pattern I have in the title, which is the ellipsis… and then delete everything. But when I do that, in the X space of my array, it returns the following:
was...
In position 8 of the array comes the word and the ellipsis and I don't know how to find a pattern to delete the author of the title, my pattern was the ellipsis. Any idea?
<?php
$a = get_the_title(155571);
$search = '... ';
if(preg_match("/{$search}/i", $a)) {
echo 'true';
}
?>
I tried with the code above and found the ellipsis, but I needed to bring it into an array to delete the part I need. I tried something like this:
<?php
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
global $wpdb;
$title_array = explode(' ', get_the_title(155571));
$search = '... ';
if (array_key_exists("/{$search}/i",$title_array)) {
echo "true";
}
?>
I started doing it this way, but it doesn't work, any ideas?
Thanks,
If you use regex you need to escape the string as preg_quote() would do, because a dot belongs to the pattern.
But in your simple case, I would not use a regex and just search for the three dots from the end of the string.
Note: When the elipsis come from the browser, there's no way to detect in PHP.
$title = 'The phrase... (author).';
echo getPlainTitle($title);
function getPlainTitle(string $title) {
$rpos = strrpos($title, '...');
return ($rpos === false) ? $title : substr($title, 0, $rpos);
}
will output
The phrase
First of all, since you're working with regular expressions, you need to remember that . has a special meaning there: it means "any character". So /... / just means "any three characters followed by a space", which isn't what you want. To match a literal . you need to escape it as \.
Secondly, rather than searching or splitting, you could achieve what you want by replacing part of the string. For instance, you could find everything after the ellipsis, and replace it with an empty string. To do that you want a pattern of "dot dot dot followed by anything", where "anything" is spelled .*, so \.\.\..*
$title = preg_replace('/\.\.\..*/', '', $title);

php preg replace include slash(/)

i use preg replace since my column database does not support "strange letters"
but after regex i need keep "/", in this code bellow "/" is always missing
in code bellow i need to get all letter complete
<?php
$jurnalName = "TL 110/90-12 K93-N02 AHM+";
$name = htmlspecialchars(htmlentities($jurnalName));
$name = preg_replace('/[^A-Za-z0-9|\- +]/', '', $name);
var_dump($name);
the result is always "TL 11090-12 K93-N02 AHM+" what i expecting is complete "TL 110/90-12 K93-N02 AHM+"
Add / to the list of chars you want to keep
<?php
$jurnalName = "TL 110/90-12 K93-N02 AHM+";
$name = htmlspecialchars(htmlentities($jurnalName));
$name = preg_replace('/[^A-Za-z0-9|\-\s\+\/]/', '', $name);
var_dump($name);
(I also changed the space for \s and scaped the plus +, its optional here but I think its a good practice for characters that have spetial meaning inside regex

preg_split : How to get what's before the split

I'm having some issues with the preg-split function.
I would like to get what is before the delimiter instead of what's after it.
I've found some leads explaining that using the following code would do the trick :
$var = end(preg_split('/\./',$string));
echo($var[0]);
But when I'm doing that I only get the first char and not every chars before the dot.
Here is my code :
$item = "software_technical_item.TI";
$joint = end(preg_split('/\./',$item));
I obviously get "TI" in $joint, I would like to get "software_technical_item", would someone know how to do that ?
Thanks,
Corentin.
Dot is a special character in regex which matches any character , you need to escape it in-order to match a literal dot.
$string = "software_technical_item.TI";
$var = preg_split('/\./',$string);
echo($var[0]);
Output:
software_technical_item

regex \p{L} problems

im using this for my Validation:
$space = "[:blank:]";
$number = "0-9";
$letter = "\p{L}";
$specialchar = "-_.:!='\/&*%+,()";
...
$default = "/^[".$space.$number.$letter.$specialchar."]*$/";
if (!preg_match($all, $input)){
$error = true;
}
The Problem i have is:
all is working except "ü"... "Ü" is but "ü" not and i dont know why?
\p{L} should accept all letters and special letters... i dont get it why its not working :(
anyone an idea what i can do?
The data i try to validate is a POST Value from a registration FORM
// p.s. if im using \p{L}ü i get an error like this:
Compilation failed: range out of order in character class at offset 23 in...
Escape the dash:
$specialchar = "\-_.:!='\/&*%+,()";
# here __^
Also add the /u modifier for unicode matching:
$default = "/^[".$space.$number.$letter.$specialchar_def."]*$/u";
# here __^
Test:
$space = "[:blank:]";
$number = "0-9";
$letter = "\p{L}";
$specialchar = "\-_.:!='\/&*%+,()";
$default = "/^[".$space.$number.$letter.$specialchar."]*$/u";
// wrong variable name ^^^^^^^^^^^^ in your example.
$input = 'üÜ is but ';
if (!preg_match($default, $input)){
echo "KO\n";
} else {
echo "OK\n";
}
Output:
OK
The problem is the position that the hyphen is placed in. Within a character class you can place a hyphen as the first or last character in the range. If you place the hyphen anywhere else you need to escape it in order to add it to your class.
$specialchar = "_.:!='\/&*%+,()-";
Also you need to add the u (Unicode) modifier to your regular expression. This modifier turns on additional functionality of PCRE and pattern strings are treated as (UTF-8) and you have the wrong variable in the pattern.
$default = "/^[".$space.$number.$letter.$specialchar."]*$/u";
The - is a special character in character class used to specify a range of character. When you construct the regex by string concatenation, it is recommended that you escape it \-.
Including the fix above, there is another problem in "\-_.:!='\/&*%+,()". Do you want to include \ and /? Or only one of them?
If you want to include both, you should specify it as "\-_.:!='\\\\\/&*%+,()".
If you don't want to escape /, you can replace your separator / in the construction of $default to something not used in your regex, for example ~. In that case, the list of special character will have one less \: "\-_.:!='\\\\/&*%+,()".

php string replace by str_replace issue

i made a function to replace a words in a string by putting new words from an array.
this is my code
function myseo($t){
$a = array('me','lord');
$b = array('Mine','TheLord');
$theseotext = $t;
$theseotext = str_replace($a,$b, $theseotext);
return $theseotext;
}
echo myseo('This is me Mrlord');
the output is
This is Mine MrTheLord
and it is wrong it should be print
This is Mine Mrlord
because word (Mrlord) is not included in the array.
i hope i explained my issue in good way. any help guys
regards
According to the code it is correct, but you want it to isolate by word. You could simply do this:
function myseo($t){
$a = array(' me ',' lord ');
$b = array(' Mine ',' TheLord ');
return str_replace($a,$b, ' '.$t.' ');
}
echo myseo('This is me Mrlord');
keep in mind this is kind of a cheap hack since I surround the replace string with empty spaces to ensure both sides get considered. This wouldn't work for punctuated strings. The alternate would be to break apart the string and replace each word individually.
str_replace doesn't look at full words only - it looks at any matching sequence of characters.
Thus, lord matches the latter part of Mrlord.
use str_ireplace instead, it's case insensitive.

Categories