How to search string in given string in smarty - smarty

I am using below code to search string in given string in smarty but below code not working
$MyError = "Attribute";
{if $MyError|strpos:'Attribute Registration Number with value'}
<p>Welcome</p>
{/if}

In Smarty, you can use the PHP function strpos to search a string in given string :
{if strpos('Attribute Registration Number with value', $MyError) !== false}
<p>Welcome</p>
{/if}
Here is an other post about it.

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';

Want to validate the part of a string shouldn't be null

I have this following element which contain a string for Practitioner, its value is 1- zzz. How to validate after - it shouldn't be null. Even if there is a string or empty. It shouldn't print null. Also want to select the value under Practitioner (currently hard coded the position of the element as 2)
<div class="styles__container___BfTYi">
<div class="styles__subHeader___18Yg1">Practitioner</div>
<div class="styles__data___1senX">1- zzz</div>
</div>
I have the following code to retrieve the text,
cy.get(pageSelector.practitionerValidator).eq(2).then(function($getText) {
let practitionerName = $getText.text();
var validateLastName = practitionerName.split(' ');
cy.log(validateLastName[1]);
expect(validateLastName[1]).to.not.equal('null');
})
You can directly check that the entire string is not null like this:
cy.get('.styles__data___1senX').then(($ele) => {
expect($ele.text()).to.not.be.null
})
Or if you want to check that your inner text is not empty you can do:
cy.get('.styles__data___1senX').then(($ele) => {
expect($ele.text()).to.not.be.empty
})
You can find the selector from the text Practitioner like this:
cy.contains('Practitioner')
.parent()
.within(() => {
cy.get('div[class*="styles__data__"]').then(($ele) => {
expect($ele.text()).to.not.be.null
})
})
Appreciate the level of detail you gave.
I will be going off this assumption.
the Practitioner string will be random can spaces or no spaces after the - (ie 34- sdfwe, 3- , 1- )
I would use a regex to check the format of the string to check the string starts with a digit followed by a dash and a space with either a string, spaces, or nothing. /\d+\-\s(\w+|\s+)?/
Your code would look a bit like this.
cy.get(pageSelector.practitionerValidator)
.eq(2)
.invoke('text') // get text
.should('match', /\d+\-\s(\w+|\s+)?/) // use regex assertion

Laravel how to replace a words in string

I am trying to change words in string, I know laravel Str::replace() function but it converts if you write exact word. This is what i want to do :
$string = " this is #username and this is #username2"
I want to find usernames which starts with "#" and make them like below, i want to add <strong> html tag them :
$newstring = " this is <strong>#username</strong> and this is <strong>#username2</strong>"
I couldnt find the way to change dynamic value on str replace.
Thanks
Use preg_replace
$string = 'this is #username and this is #username2';
$newString = preg_replace('/(\#\w+)/', '<strong>$1</strong>', $string);
Docs: https://www.php.net/manual/en/function.preg-replace.php

Check text area for URLs with preg_match

i found this code for checking a text area:
if (strpos($textp, "http://") !== false ||
ereg("(www.[a-zA-Z0-9_-]+)\.([a-zA-Z0-9.]+)",$textp)){
$textp = '';
} else {
because ereg is depreacted I would like to replacy by preg_match instead. But if I replace I got an error according to \
So what I should I exactly edit to get the same result - no URLs or elements of that inn that text area.
you just need to add / like
preg_match("/(www.[a-zA-Z0-9_-]+)\.([a-zA-Z0-9.]+)/",$textp)
please refer to preg_match

Smarty false is true?

I have a function for smarty:
function smarty_function_false_() {
return false;
}
then if I do:
{if false_} false YES?? {else} false no {/if} <br>
{if not false_} not false yes {else} not false NO??? {/if}
And it evals to:
false YES??
not false NO???
I'm new to smarty, why is that happening?
The Smarty expression {if false_} does not evaluate any functions. It is equivalent to {if 'false_'}, which is in turn just the same as the PHP expression if ( 'false_' ). (Incidentally, if ( false_ ) in PHP also means the same thing, unless you have run define('false_', ...).)
Under PHP's "type juggling" rules, a string interpreted as a boolean is true as long as it is not the empty string (''). So {if false_} is equivalent to {if true}.
A Smarty "template function" is designed only to be called on its own, and return something to output to the template, e.g. {false_}.
The easiest way to have a callback that you can check inside an {if} condition is to define a "modifier" rather than a "function". Although a modifier will always be given at least one parameter, it can simply be ignored, so you could have the following:
function smarty_modifier_false_($whatever) {
return false;
}
And then in Smarty just pass any old string as the left of the modifier:
{if ''|false_} false_ is true!? {else} false_ is false. How reassuring. {/if}
Alternative approaches:
Set the Security settings in your template such that you can write {if false_()}, because false_ is a PHP function allowed directly in templates. (See documentation on {if} where it links to the details of Security.)
Register a block function. These are a little trickier to write, but would allow you to have your own custom if replacement, e.g. {if_false_} This text never appears {/if_false_}.

Resources