Jump to content

Which Php Function Checks For Strings That CONTAIN or NOT CONTAIN Certain Chars ?


Go to solution Solved by requinix,

Recommended Posts

	$test = array('id','date_and_time','kw_1','kw_1_point','kw_2','kw_2_point','kw_3','kw_3_point','kw_4','kw_4_point');
	

Q2.

How to check all the values to see if any of them  DOES contain '_'.

How to do a wildcard check with php here ? No need to echo anything here like the previous question.

Edited by TheStudent2023
Link to comment
Share on other sites

22 hours ago, requinix said:

array_filter + str_contains

Manual is too complicated. So trying this website instead:

https://www.w3schools.com/php/func_array_filter.asp

But it does not explain what a callback is. what is it ? Can you show me an example ?

And, I do not understand this code atall because the $var has not been defined:

	<?php
function test_odd($var)
  {
  return($var & 1);
  }

$a1=array(1,3,2,3,4);
print_r(array_filter($a1,"test_odd"));
?>
	

What is the value of $var ?

Link to comment
Share on other sites

I strongly suggest that you not use W3Schools for anything.  The PHP manual is the best resource you have.

So - you went to W3 and now you don't understand what they gave you.  Gee.  If you don't understand that very-simple-code then LOOK IT UP IN THE MANUAL.  Hint: the line of code is using a bitwise operator.

yes - I am being very hard on you.  But I hate it when people join a programming forum and then refuse to do the work that is necessary.  If you find it too hard then perhaps you are in the wrong world.

As for the code it does work.  If you know how to read the results.  Try this:

$a1=array(1,3,2,3,4);
echo "<pre>";
print_r(array_filter($a1,"test_odd"));
echo "</pre>";
exit();
function test_odd($var)
{
	return($var & 1);
}

The code is asking for the Odd entries in that sample array.  That's what it gives you - the indices of the odd elements.  You could add some new code to read the results to actually output the real values from the array by using those indices in a loop to output the values instead of the indices.  That's too much work for me.

Link to comment
Share on other sites

21 hours ago, ginerjm said:

I strongly suggest that you not use W3Schools for anything.  The PHP manual is the best resource you have.

So - you went to W3 and now you don't understand what they gave you.  Gee.  If you don't understand that very-simple-code then LOOK IT UP IN THE MANUAL.  Hint: the line of code is using a bitwise operator.

yes - I am being very hard on you.  But I hate it when people join a programming forum and then refuse to do the work that is necessary.  If you find it too hard then perhaps you are in the wrong world.

As for the code it does work.  If you know how to read the results.  Try this:

$a1=array(1,3,2,3,4);
echo "<pre>";
print_r(array_filter($a1,"test_odd"));
echo "</pre>";
exit();
function test_odd($var)
{
	return($var & 1);
}

The code is asking for the Odd entries in that sample array.  That's what it gives you - the indices of the odd elements.  You could add some new code to read the results to actually output the real values from the array by using those indices in a loop to output the values instead of the indices.  That's too much work for me.

Ginerjm

 

I did check But where is the $var defined ? That was my original question. What is the value of $var to begin with ?

Link to comment
Share on other sites

33 minutes ago, TheStudent2023 said:

I did check But where is the $var defined

$var is defined as a function parameter.  I suspect that what you're trying to ask is what value it holds since you don't see it getting called anywhere.  It'll be called by array_filter, and if you read the documentation for that function it tells you what the value of $var will be.

Quote

Iterates over each value in the array passing them to the callback function.

 

  • Like 1
Link to comment
Share on other sites

21 minutes ago, kicken said:

$var is defined as a function parameter.  I suspect that what you're trying to ask is what value it holds since you don't see it getting called anywhere.  It'll be called by array_filter, and if you read the documentation for that function it tells you what the value of $var will be.

 

@kicken

I did read the manual. Did not understand what is callback.

Reading again.

So, what is callback ?

Edited by TheStudent2023
Link to comment
Share on other sites

@mac_gyver

Frankly, I do not understand the function syntaxes.

	array_filter(array $array, ?callable $callback = null, int $mode = 0): array
	

What is the above explaining ? If I can get the hang of understanding things like this then a lot of questions I won't need to ask.

I mean, should not the syntax been like this:

	array_filter(array, ?callable, int): array
	

And the example, like this:

	array_filter($array, ?$callback = null, $mode = 0): array
	

But mixing the above 2 up and making following is confusing syntax:

	array_filter(array $array, ?callable $callback = null, int $mode = 0): array
	
Edited by TheStudent2023
Link to comment
Share on other sites

It is telling you (the PHP manual, that is) that the function needs an array to work with, a function to be called as the array is processed and what gets returned from all the work back to the user.  And that is Exactly what the code I wrote and gave to you shows you if you simply read it and make effort to understand it.

Link to comment
Share on other sites

1 hour ago, TheStudent2023 said:

Frankly, I do not understand the function syntaxes.

Then perhaps you should read the manual section entitled How to read a function definition (prototype).

1 hour ago, TheStudent2023 said:

So, what is callback

And the section entitled Callbacks / Callables.

When you're new to something, you need to spend time on reading about the fundamentals.  That way when you look at the detailed references you understand them.  There's a lot to the PHP manual besides the function references for you to read and learn from.

 

  • Thanks 1
Link to comment
Share on other sites

2 hours ago, ginerjm said:

It is telling you (the PHP manual, that is) that the function needs an array to work with, a function to be called as the array is processed and what gets returned from all the work back to the user.  And that is Exactly what the code I wrote and gave to you shows you if you simply read it and make effort to understand it.

Sorry, mate. Sometimes your sentences do not make sense due to grammatical errors. Also, you do not put commas in the right places. And so, I find it hard to understand your sentences. I always understand your replies 50-75%. Never 100%.

Gonna re-read your sentences a few times to see what you are trying to tell me.

Link to comment
Share on other sites

1 hour ago, kicken said:

Then perhaps you should read the manual section entitled How to read a function definition (prototype).

And the section entitled Callbacks / Callables.

When you're new to something, you need to spend time on reading about the fundamentals.  That way when you look at the detailed references you understand them.  There's a lot to the PHP manual besides the function references for you to read and learn from.

 

Thanks. I do understand you, though.

It's just, back in late 2015, I started learning php from php.net. Then found it too complicated. Tried tutorial sites instead and learnt the very basics. Enrolled to php class but before long the teacher quit his job and gave his brother to take-over his position. Brother was able to teach all the other subjects, like css. But he did not know php and I found myself in a position that, I will have to teach him instead. I quit the school. And found myself dropped in the middle of the sea. Not knowing which direction to swim for (where the shore is). Put php on hold till early 2017. Stuck to it since.

So, by myself at home, started learning php but not in proper order. That is why I do not know some fundamentals. Got no proper guidance. I just learn by reading tutorials here and there online and you know very well most tutorials are outdated. Found that out the hard way. Had I known pdo was new then I never would have bothered with mysqli. That is one example.

Thanks for the links.

Your link begins with:

"Each function in the manual is documented for quick reference. Knowing how to read and understand the text will make learning PHP much easier. Rather than relying on examples or cut/paste, everyone should know how to read function definitions (prototypes)."

They took words right out of my mouth! Because that is what I have been doing for 6yrs now. I not know how to read the functions syntax explanations in the manual and always rely on code examples to figure-out how the functions work.

Edited by TheStudent2023
Link to comment
Share on other sites

@kicken

 

Don't mind. But can you see if I understood the tutorial or not ?

	Description
	strlen ( string $string ) : int
	

Q1.

The data on the right of the colon means what type of data will be returned by the function. Yes ? Finally, I learnt what the colon means on function syntax explanations!

The manual ads:

"We could rewrite the above function definition in a generic way:

function name ( parameter type parameter name ) : returned type"

Yes. The manual should have shown this generic way in all their functions explanation pages as it is easier to understand!

 

Q2.

in_array ( mixed $needle, array $haystack , bool $strict = false ) : bool

The tutorial does not explain what the $ mean here. These 6 yrs, when I look at a function syntax and see the $, I think they are $vars. But now, I understand that they are not. The $ indicate the word following it is the function parameter name (and not a variable name that can be inputted in the parameter). Correct ?

Silly sods! They should have CAPITALISED the parameter names instead. Like so ATLEAST:

in_array ( mixed NEEDLE, array HAYSTACK , bool STRICT = false ) : bool

Better, had they wrote the function syntax like this:

in_array ( mixed NEEDLE, array HAYSTACK , bool STRICT = false ) : bool

This is where the italic represents "data type", CAPITAL represents "param name". Underline represents "default value".

Now, is not this a better way to get the message through to others ? Yes.

I always knew I was never an inventor but always an improver of inventions. That is my strength. I can figure-out how to simplify things. I am not a good learner. But, whatever I manage to learn with hiccups (by struggling), I can easily teach others in a better simplified way that gets them to easily learn the thing and remember it and not forget it. Once a student 21yrs ago, remarked I teach better than the teacher himself. I agree.

Thanks!

Edited by TheStudent2023
Link to comment
Share on other sites

2 hours ago, TheStudent2023 said:

Yes. The manual should have shown this generic way in all their functions explanation pages as it is easier to understand!

I'm not sure what you mean by this.  If you mean they should show that generic example along with the specific details on every reference page, then that's a terrible idea as that would create a bunch of unnecessary clutter.  Once you know how to read a prototype, you don't need that generic reference explaining the prototype everywhere.  If you don't know how the read the prototype, then you find that page of the manual which tells you and read it first.

2 hours ago, TheStudent2023 said:

The tutorial does not explain what the $ mean here. These 6 yrs, when I look at a function syntax and see the $, I think they are $vars.

It does mean variable, essentially. It's stylized with the $ because that's how things work in PHP when you write your own functions.  If you wanted to write your own in_array function for example, you'd declare it like so:

function my_in_array(mixed $valueToFind, array $arrayToSearch, bool $strictSearch = false) : bool {
    //TODO: implement me
}

See how the code you'd write pretty much matches exactly the prototype in the manual?  That's why the manual documents the signatures the way it does.  Your suggested "improvements" are not improvements at all.  Making the name upper-case doesn't improve things.  Neither does adding underlines or italics.

The names themselves are mostly just informational, they don't mean you have to pass a variable with that exact name.  You can pass whatever variable (or constant) value you want.  The names only become somewhat relevant with the introduction of Named Arguments in PHP 8.0.  Most functions don't benefit from the named arguments feature, so using it is unnecessary.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.