Counting preg_match in smarty - preg-match

How to count preg_match matches in smarty? Why is the following not working?
{preg_match("/\[PGN.*](.*)\[\/PGN.*\]/", $code, $match)}
{$match|#count}

As far as I understand your needs, the following does work, just make the regex not greedy:
preg_match_all("~\[PGN\d+](.+?)\[/PGN\d+]~", $code, $match)

Related

Laravel validation for numbers and hyphens

How do you validate the numeric values with Hypen's (-) ?
I just want to validate the numeric value with Hypen's and without Hypen's.
Thank you so much !
For example 555 or 5-55
You may use preg_match with the regex pattern ^[0-9-]+$:
$input = "867-5309";
if (preg_match("/^[0-9-]+$/", $input)) {
echo "MATCH"; // prints MATCH
}
I suggest for this purpose from the site:
https://www.phpliveregex.com/
use
For example, I wrote a preg_match in the link: ‌
https://www.phpliveregex.com/p/Ep6
You can see
My suggested code:
$input = "867-5309";
echo preg_match("/^[0-9-]+$/", $input) ? 'MATCH' : 'Not MATCH';

Get first letter of word in statement

I have a statement like "Animal Association" from the database. I want to get its short form. It means, only the first letter of each word like this "AA". In the blade file, I got the whole statement as follows,
<p>{{ $animal->user->club->name}}</p>
So, how can I get a short form of this name?
Thank You!
If you are using MySQL 8+, then a raw select with REGEXP_REPLACE should work here:
$users = DB::table('animals')
->select(DB::raw("SELECT REGEXP_REPLACE(name, '(\\w)\\w+\\s*', '$1')"))
->get();
This very common problem where we ran into, I can provide you a function that will solve your problem. I am sharing two solutions and you can use any of these solutions.
using function
You can use this function in your model and solve your problem.
public function getNameAbbreviate($string){
$abbreviation = "";
$string = ucwords($string);
$words = explode(" ", "$string");
foreach($words as $word){
$abbreviation .= $word[0];
}
return $abbreviation;
}
There is probably no one-line solution, the solution which I provided is readable and understandable.
using regex
This solution is easy to apply and in case you can't make the first method work then go with this.
<p>{{ preg_split("/\s+/", $animal->user->club->name) }}</p>
using regex we can get a direct solution but I personally don't like it or recommend it.

how to display single value use arrayshit or other thing (without loop) in Laravel

I use display value or data. In case of a single value use loop, but I know this is a bad habit. How can I solve this? for example I use this:
#foreach($result as $result)
{{$result->data}}
#endforeach
First of all, your code doesn't tell anything, and also you have this code all wrong.
You can't name a pointer as the same name of the array, ($result as $result ???) you will have wrong results or errors.
Also inside the froeach you are referencing a variable that doesn't exists (?).
In my opinion "foreach" is necessary to loop through arrays.
I really don't know what are you trying to do, so that would be great if you expand your question and provide more information.
You can replace that with some code like this:
#if($result)
{{$data}}
#endif
You can try code below see
//example in file ExampleController.php
use App\Posts;
public function showAll(){
$data = Posts::all();
return view('showall',['result'=>$data]);
}
//file web.php
Route::get('/show-all','ExampleController#showAll');
//show-all.blade.php
#foreach($result as $value)
{{$value.title_name}}
#endforeah

typo3 extension formhandler pregmatch

I am trying to validate a iban no for germany, but I cannot get pregMatch to work with formhandler. I cannot find a mistake and compared it with the formhandler documentation, but nothing helped. Maybe somebody has a hint for me.
This is my code:
debitiban.errorCheck {
1 = pregMatch
1.value = ^DE\d{2}\s?([0-9a-zA-Z]{4}\s?){4}[0-9a-zA-Z]{2}$
}
The value is directly passed on to PHPs preg_match function, so it has to be a valid PCRE regex pattern. You are missing a delimiter charater in your expression. Try to surround the expression by slashes:
debitiban.errorCheck {
1 = pregMatch
1.value = /^DE\d{2}\s?([0-9a-zA-Z]{4}\s?){4}[0-9a-zA-Z]{2}$/
}

Printing this variable in smarty

I'm having an array $customPre. I want to print the element of the array "Please specify which fund". I am doing like this:
{$customPre.Please specify which fund}
But it's not working.
In this case you need to use PHP-like syntax that is similar to PHP: {$variable['key']}.
If In PHP you have:
$smarty->assign('customPre', array ('Please specify which fund' => 'This is value'));
In Smarty you need to use:
{$customPre['Please specify which fund']}
And the output for this will be:
This is value
I believe you cannot use in this case dot syntax ( {$customPre.Please specify which fund}) because it's probably looks for whitespaces in keys. Even adding quotes won't help.

Resources