How would I go about searching/matching for a paticular set of a characters in a filename, for example 3XYTPRQgz.pdf is the filename, I need to search for '3XYTPRQ', then if this string is found I simply want to output 'job completed', if its not there it will be set to queued. ( I want to do this for more than one file).
My thoughts on how to do this is, (I am struggling on the matching of the string part) :
<?php
if(match("7digitnumber) //then < not sure what function to use any tips?
if file_exists($7digitnumber/filename)
{
echo "completed";
}
else
{
echo "queued";
}
?>
Thanks for any help.
If you simply want to find out if a given string (your case "3XYTPRQ") is part of a longer string ("3XYTPRQgz.pdf") you can take a look at strstr.
Related
I currently use:
if(strpos($command->href,§current_view) !== false){
echo '<pre>true</pre>';
} else {
echo '<pre>false</pre>';
}
$command->href will output something like this: /path/index.php?option=com_component&view=orders Whereas
§current_view is outputting orders. These outputs are dynamically generated, but the scheme will always be the same.
What I need to do is return true/false if the words from $current_view match the view=orders in the URLs from $command->href. The issue with my code is, that it doesnt match anything.
What is the correct way to do this?
Please note that the $command->href and the whole code is inside a while function, that pass multiple URLs and this only needs to match the same ones.
Breaking it down to a simple example, using your code and variable values.
$current_view = 'orders';
$command = '/path/index.php?option=com_component&view=orders';
if(strpos($command,$current_view) !== false){
echo '<pre>true</pre>';
}
else {
echo '<pre>false</pre>';
}
The oputput is "true".
Now, go and debug the REAL values of $command->href and $current_view...
I'm pretty confident that the values are not what you think they are.
Does something like:
if(substr($command->href, strrpos($command->href, '&') + 6) == $current_view)
accomplish what you are after?
To explain, strpos get the last instance of a character in a string (& for you, since you said it always follows the scheme). Then we move over 6 characters to take "&view=" out of the string.
You should now be left with "orders" == "orders".
Or do you sometimes include some arguments after the view?
Try parsing url, extracting your view query string value and compare it to $current_view
$query= [];
parse_str(parse_url($command->href)['query'], $query);
if($current_view === $query["view"])
echo '<pre>true</pre>';
} else {
echo '<pre>false</pre>';
}
I did some research and couldn't find a clear answer to my problem. This is what I have:
<?php
session_start();
$gen_num = file_get_contents($_SESSION['filename']);
$inp_num = $_POST['form-input'];
if($gen_num === $inp_num){
echo "Yes! The numbers match!";
} else {
echo "No, the numbers do not match…";
}
?>
Where the 'filename' has a string of numbers and 'form-input' is carried from a previously submitted HTML form.
Why does the IF test fail when the strings are identical?
EDIT
The simple fix was trimming the $gen_num. Also, I wasn't specific enough in saying that the 'filename' was a .txt file that included a string of numbers along with some unseen special characters.
Thank you for the help!
If $_SESSION['filename'] is as you say a string of numbers then there is no file name ending.
You should make that line:
$gen_num = file_get_contents($_SESSION['filename'] . ".txt");
Or file_get_contents won't find a file with only numbers as the name and return false.
That is why the numbers don't match.
If the two values are integers I'd floor() them and use ==
And catch for 0 exception
Suppose, I have a string '#[52:] loves his mother very much'. I want the string to be replaced with 'Allen loves his mother very much'.
Explanation: when any match found in my string with syntax '#[numeric_id:]' then these matches will be replaced with the name of the user exist with the 'numeric_id' in 'user_entry' table. If match found in the string but no user found with the numeric_id in 'user_entry' table then it will return the exact string like '#[52:] loves his mother very much'.
I tried to do it with 'preg_replace' function in php. 'preg_replace' successfully collects all matches with syntax '#[numeric_id:]' but it can't send matches to a user defined function named 'test()'. In short my code does not working.
I have the following code in test.php file: .
<?php
function test($v) { $con=mysqli_connect("mysql14.000webhost.com","a8622422_jhon","pjdtmw7","a8622422_person");
$safe_id=preg_replace("/[^0-9]/",'',$v);
$sql="SELECT * FROM user_entry WHERE u_id='$safe_id'";
$result=mysqli_query($con,$sql);
$count=mysqli_num_rows($result); $found='';
if ($count==1) {
$row=mysqli_fetch_array($result);
$found=$row['name'];
} else { $found=$v; } return $found;
} ?>
<?php
$post='#[61150631867349144:] & #[59670019475743176:] are friends';
echo preg_replace('/(#\[[0-9]+\:+\]+)/',test('$1'),$post);
?>
I think, the code should return: 'Baki Billah, Mahfuzur rahman and mahi are friends'. But it returns: '#[61150631867349144:] & #[59670019475743176:] are friends'.
How can I do that? What's wrong with my code? Is there any way to do it? If it is impossible to do the action with above code, then please give me the correct & full code of test.php file so that I can do the action explained in the first part of my question.
Any help will be strongly appreciated (I'm working with php).
Thanks in advance.
If you want to execute code for each match of a regular expression the best way is to use the preg_replace_callback function. It can take the same pattern as you are using now but will call a given function for each match.
For example:
echo preg_replace_callback('/(#\[[0-9]+\:+\]+)/', 'test', $post);
You will need to modify your test function the receive an array of sub-matches. $v[0] will be the entire string match.
I am currently trying to get my head around some basic php string functions. I currently use this code which determines if the username entered in long enough e.g.:
if (strlen($_GET['name']) < 3) {
echo 'First Name should be at least 3 characters long!';
exit;
}
And this works just fine. Which string function should I use though if I want to to check on a specific name? E.g. I would like to trigger a message once someone enters a specific Word in the form field.
Some expert advice would be greatly appreciated.
This link of 60 PHP validation functions is an excelent resource.
For your case as to check a name, you could use something like:
if (strtolower($_GET['name']) === 'joe') {
// Do something for Joe
}
elseif (in_array(strtolower($_GET['name']), array('dave', 'bob', 'jane')) {
// Do something else for Dave, Bob or Jane
}
The strtolower will ensure that upper, lower or mixed case names will match.
You don't need a function for that. You can use a if statement and ==:
if ( $_GET['name'] == 'Dave' )
{
// user entered 'Dave'
}
if statement, or if you plan to check against multiple names, switch().
switch($_GET['name']){
case "Eric":
//Eric
break;
case "Sally":
//Sally
break;
case "Tom":
//Tom
break;
default:
//Unknown
}
Its good practice to check that $_GET['name'] is set before using. To answer your question a good way IMO is in_array(needle,haystack)
<?php
if (!empty($_GET['name']) && strlen($_GET['name']) < 3) {
echo 'First Name should be at least 3 characters long!';
exit;
}
//From a database or preset
$names = array('Bob','Steve','Grant');
if(in_array($_GET['name'], $names)){
echo 'Name is already taken!';
exit;
}
?>
You can use strstr or stristr(case-insensitive) function, If want to search for specific word in a sentence.
Just check php mannual for strstr, and stristr.
I am building a gallery in WordPress and I'm trying to grab a specific part of my URL to echo into the id of a div.
This is my URL:
http://www.url.com/gallery/truck-gallery-1
I want to isolate the id of the gallery which will always be a number(in this case its 1). Then I would like to have a way to print it somewhere, maybe in the form of a function.
You should better use $_SERVER['REQUEST_URI']. Since it is the last string in your URL, you can use the following function:
function getIdFromUrl($url) {
return str_replace('/', '', array_pop(explode('-', $url)));
}
#Kristian 's solution will only return numbers from 0-9, but this function will return the id with any length given, as long as your ID is separated with a - sign and the last element.
So, when you call
echo getIdFromUrl($_SERVER['REQUEST_URI']);
it will echo, in your case, 1.
If the ID will not always be the same number of digits (if you have any ID's greater than 9) then you'll need something robust like preg_match() or using string functions to trim off everything prior to the last "-" character. I would probably do:
<?php
$parts = parse_url($_SERVER['REQUEST_URI']);
if (preg_match("/truck-gallery-(\d+)/", $parts['path'], $match)) {
$id = $match[1];
} else {
// no ID found! Error handling or recovery here.
}
?>
Use the $_SERVER['REQUEST_URI'] variable to get the path (Note that this is not the same as the host variable, which returns something like http://www.yoursite.com).
Then break that up into a string and return the final character.
$path = $_SERVER['REQUEST_URI'];
$ID = $path[strlen($path)-1];
Of course you can do other types of string manipulation to get the final character of a string. But this works.