What's the mb_substr() equivalent? - twig

Please take a look at the following line of PHP code:
<?php echo mb_substr($logged_text, 0, 12, "utf-8"); ?>
How can I write it using Twig?
There is a type of filter in Twig named slice, but I want a thing that handles utf-8

Related

How not to escape path() in Twig Template

My objective is to render a Twig Template and send the resulting HTML via API to Mailchimp to be sent out.
My current process:
1) create a Twig-Template email.html.twig.
2) $html = $this->renderView('MyBundle:email.html.twig');
3) sendHtmlViaApi($html);
The issue:
I need a URL to contain a Mailchimp Merge Tag String, which has to be *|VARIABLE|*. I do that with {{ path('my_route', {variable : '*|VARIABLE|*'}) }}. The desired result: /myroute/*|VARIABLE|*. The result I get: /myroute/*%7CVARIABLE%7C*.
Already tried and failed methods:
1) using {% autoescape %}
2) |raw
3) Twig Extension with new url_decode Filter from Symfony2 Twig stop escaping path
So you want Twig to stop the automatic URL encoding.
You can pass a placeholder with only letters and underscore to path(), so that it won't be escaped. Then you can replace the placeholder with the string Mailchimp expect:
{{ path('my_route', {variable : 'MAILCHIMP_VARIABLE'})|replace({
'MAILCHIMP_VARIABLE': '*|VARIABLE|*'
}) }}
Thanks for your suggestions!
In the end it was all my own fault... One of the merge tags was missing on the mailchimp-side setup, so it couldn't replace it with the desired value.
Silly me!

Parses input from a string according to a format with Powershell?

I'm new to Powershell and I wonder if there is any way to parse input from a string according to a format, just like the sscanf() function in PHP?
URL Refered
The PHP sscanf can be emulated with matching the input with a regex. As per the PHP manual page example, let's parse author data into XML like so,
$auth = "24`tLewis Carroll" # ` is the escape char in PSh
$mc = [regex]::Match($auth, "(\d+)\t(\w+)\s(\w+)")
write-host $("<author id='{0}'>`n <firstname>{1}</firstname>`n <surname>{2}</surname>`n</author>" -f $mc.Groups[1].Value, $mc.Groups[2].Value, $mc.Groups[3].Value)
Output
<author id='24'>
<firstname>Lewis</firstname>
<surname>Carroll</surname>
</author>

Get the image url from string

My string is: [slide image="http://themes.devatic.com/konzept/wp-content/themes/konzept/includes/uploadify/uploads/bas_006.jpg" slide_desc="
How to get the part after "image=" from it?
In short, use RegEx to get the data between 2 strings. For the most part, the same Reg Expressions between different languages should work just fine.
In PHP, you would want to use a preg_match.
$string = "[slide image=\"http://themes.devatic.com/konzept/wp-content/themes/konzept/includes/uploadify/uploads/bas_006.jpg\" slide_desc=\""
preg_match("/image=\"(.*)\"/i", $string, $results)
var_dump($results)
Update regex (a little more strict):
/image\="([^"]*)"/i
All in all, going to depend on the language being used and in what context. You can get much more advanced with the RegEx, but this is just quick & dirty.
Full PHP Code Example:
<?php
$string = '[slide image="http://themes.devatic.com/konzept/wp-content/themes/konzept/includes/uplo‌​adify/uploads/bas_006.jpg" slide_desc="<h4>Promotional Package</h4> Project description sentence" text_color="#464646" slide_horizontal="false"] [slide image="http://themes.devatic.com/konzept/wp-content/themes/konzept/includes/uplo‌​adify/uploads/bas_005.jpg" slide_desc="<h4></h4>" text_color="#464646" slide_horizontal="false"]';
preg_match_all('/image\="([^"]*)"/i', $string, $results);
foreach ($results[1] as $res):
echo 'Image URL:'.$res."\n";
endforeach;
?>

How to compare 2 strings if one string is plain and second contains some html tags

I have some line of statements in my database.Whenever I create an article and add any line of these statements in this article manually then I have to find these lines and make them link.
problem is that I am using ckeditor for article posting and user can add style to text also. So I have to maintain style of matched line and also make that link.
statements (saved in a table):
1: this is first line
2: this is second line
3: this is third line
Article to be created :
this is my article , this is <span style="color:red">first</span> line. rest of article.
Now when user add this article then (1) line should be matched and in article it should be appear as :
this is <span style=".">first</span> line
Hope I stated my problem clearly.
Thanks in advance.
Try this
string mysql_real_escape_string ( string $unescaped_string [, resource $link_identifier ] )
With PHP, this should get you started:
$string_without_tags = strip_tags($string_with_tags);
PHP.net strip_tags Manual
Remove the html tags. Then compare. You can use the following javascript to remove.
public static String html2text(String html) {
return Jsoup.parse(html).text();
}
Pattern for this is :
$pattern = "/(<?.*>)?this(<\/?.*>)? (<?.*>)?is(<\/?.*>)? (<?.*>)?first(<\/?.*>)? (<?.*>)?line(<\/?.*>)?;
preg_match_all($pattern,$str,$matches);
$replacement="<a href='#'>".$matches[0][0]."</a>";
$str = preg_replace($pattern,$replacement,$str,-1);

BBcode parsing problem

I use this function for BBcode parsing:
function bbcode ($message) {
$search = array(
'#\[(?i)b\](.*?)\[/(?i)b\]#si',
'#\[(?i)i\](.*?)\[/(?i)i\]#si',
'#\[(?i)u\](.*?)\[/(?i)u\]#si',
'#\[color=rgb(.*?)\](.*?)\[\/color\]#si',
'#\[quote](.*?)\[\/quote\]#si',
'#\[li](.*?)\[\/li\]#si',
'#\[ul](.*?)\[\/ul\]#si',
);
$replace = array(
'<b>\\1</b>',
'<i>\\1</i>',
'<u>\\1</u>',
'<span style=\"color:rgb\\1\">\\2</span>',
'<span class=\"quote">\\1</span>',
'<li>\\1</li>',
'<ul>\\1</ul>',
);
return preg_replace($search , $replace, $message);
}
In most cases it works ok, but not always.
For example:
[color=rgb(102, 0, 102)]H[color=rgb(204, 0, 0)]e[/color]llo[/color]
The result is:
<span style="color:rgb(102, 0, 102)">H[color=rgb(204, 0, 0)]e</span>llo[/color]
As you can see, only the first [color=...][/color] has been converted to html. The second stays as it is. Any ideas?
It's working correctly as you specified it. The problem is with embedded sequences.
I suggest you perform two replaces. One for the starting tags and one for the ending tags.
You might also be able to get away with specifying all of the starting tags first and
all of the ending tags last in the array of replacements.
That makes the search-replace values simpler anyway and in most cases you don't
need to use back-references, especially for simple tags like [b].
That should fix your problem.

Resources