Is there a way to trim newlines out of a twig string? - symfony

I'm using objects in my view that I recover from a iCalendar file, these objects have string attributes, named description, and some of these strings contain "\n", and for some multiple times and I can't seem to find a way to trim them out.
I've tried to trim them using the php function in the controller, however they still are there after the function.
Same for the trim function in Twig.
I am maybe using wrong parameters in these functions.
{{ coursCourant.description|split("(")[0]|split(".")[0] }} <BR>
This displays something like "\n\nPromo S4\n" when the expected result is the same thing but without the multiple "\n".
I'm using splits on a ( and on a . because some strings contains them and I don't need to display the parts after those.

trim will only remove trailing and ending whitespaces.
Use str_replace in your controller (recommended)
$content = str_replace("\n", '', $content);
Use replace in your template
{{ foo|replace({"\n":'',}) }}
hence the double quotes to remove an actual newline character

Related

get only numbers inside parenthesis filter or custom filter or what?

The string is "Some Words(1440)" and I want to store the numbers inside the parenthesis as a variable in twig so it can be output and used. I thought maybe I could do it with a split but I wasn't able to escape the parenthesis properly.
What I have:
Some Words (1440)
What I want to extract from the string is just the numbers in parenthesis
1440

Symfony AsciiSlugger remove first special character in a string

I have a question about the string component. I am trying to create a download link using HeaderUtils::makeDisposition. I wanted to remove all special characters from the filename using AsciiSlugger. Unfortunately, if such a character is in the first position, it is removed
If the name contains only special characters, the function returns an empty string.
$response->headers->set('Content-Disposition',
HeaderUtils::makeDisposition(
HeaderUtils::DISPOSITION_ATTACHMENT,
(new AsciiSlugger('de'))->slug('äffgößdfö')
)
);
And I expected, that from (new AsciiSlugger('de'))->slug('äffgößdfö') I become aeffgoessdfoe not ffgoessdfoe.
Does anyone know how to prevent this behaviour?

How to remove up to a certain punctuation when there are same punctuations [duplicate]

I'm new at regular expressions and wonder how to phrase one that collects everything after the last /.
I'm extracting an ID used by Google's GData.
my example string is
http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123
Where the ID is: p1f3JYcCu_cb0i0JYuCu123
Oh and I'm using PHP.
This matches at least one of (anything not a slash) followed by end of the string:
[^/]+$
Notes:
No parens because it doesn't need any groups - result goes into group 0 (the match itself).
Uses + (instead of *) so that if the last character is a slash it fails to match (rather than matching empty string).
But, most likely a faster and simpler solution is to use your language's built-in string list processing functionality - i.e. ListLast( Text , '/' ) or equivalent function.
For PHP, the closest function is strrchr which works like this:
strrchr( Text , '/' )
This includes the slash in the results - as per Teddy's comment below, you can remove the slash with substr:
substr( strrchr( Text, '/' ), 1 );
Generally:
/([^/]*)$
The data you want would then be the match of the first group.
Edit   Since you’re using PHP, you could also use strrchr that’s returning everything from the last occurence of a character in a string up to the end. Or you could use a combination of strrpos and substr, first find the position of the last occurence and then get the substring from that position up to the end. Or explode and array_pop, split the string at the / and get just the last part.
You can also get the "filename", or the last part, with the basename function.
<?php
$url = 'http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123';
echo basename($url); // "p1f3JYcCu_cb0i0JYuCu123"
On my box I could just pass the full URL. It's possible you might need to strip off http:/ from the front.
Basename and dirname are great for moving through anything that looks like a unix filepath.
/^.*\/(.*)$/
^ = start of the row
.*\/ = greedy match to last occurance to / from start of the row
(.*) = group of everything that comes after the last occurance of /
you can also normal string split
$str = "http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123";
$s = explode("/",$str);
print end($s);
This pattern will not capture the last slash in $0, and it won't match anything if there's no characters after the last slash.
/(?<=\/)([^\/]+)$/
Edit: but it requires lookbehind, not supported by ECMAScript (Javascript, Actionscript), Ruby or a few other flavors. If you are using one of those flavors, you can use:
/\/([^\/]+)$/
But it will capture the last slash in $0.
Not a PHP programmer, but strrpos seems a more promising place to start. Find the rightmost '/', and everything past that is what you are looking for. No regex used.
Find position of last occurrence of a char in a string
based on #Mark Rushakoff's answer the best solution for different cases:
<?php
$path = "http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123?var1&var2#hash";
$vars =strrchr($path, "?"); // ?asd=qwe&stuff#hash
var_dump(preg_replace('/'. preg_quote($vars, '/') . '$/', '', basename($path))); // test.png
?>
Regular Expression to collect everything after the last /
How to get file name from full path with PHP?

How to remove HTML tags in Drupal 8

$variables['slide_one_description'] = wordwrap(theme_get_setting('slide_one_description','metroman'), 20,"<br>");
In the above, I set the variable for slider description, I just want to apply line break for after some length, but the above code print HTML br tag instead of line break.
Note: description is a dynamic content and I want to solution in Drupal 8.
There used to be functions such as SafeMarkup::checkPlain() that performed this function, but they have been largely deprecated and removed. Most of the time, Twig will do your text sanitization for you, but there are times where you just need an HTML-stripped value in PHP in Drupal.
For example, PHP has a [strip_tags()][1] function that does exactly what the name implies.
Or, for a more Drupal-friendly way, just pass the value through a t() function and use a # replacement to force plain text. Here's an example of how it works:
$value = '<p>Here is a <b>value</b> with HTML.<br>Let's remove it.</p>';
$stripped_value = t('#value', ['#value' => $value]);
This is a trivial example, of course, but you can apply it to other scenarios. Also, this just gets rid of <br> tags, rather than replacing them with line breaks; you'd need a PHP function like the following to replace <br> tags with something else:
function br2nl($string)
{
return preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string);
}

asp.net regex help

Hi ive got this regular expression and that extracts numbers from a string
string.Join(null,System.Text.RegularExpressions.Regex.Split(expr, "[^\\d]"));
so eg, the format of my string is like this strA:12, strB:14, strC:15
so the regex returns 121415
how can I modify the expression to return
12,14,15 instead, any suggestions please
You're calling String.Join, which joins an array of strings into a single string, separating each element by the separator parameter.
Since you're passing null as that parameter, it doesn't put anything between the strings.
You need to pass ", " instead of null to separate each string with ,.

Resources