how to get the spesific word from different string in php - php

for getting spesific word that i will use it for search term.
if i have a random word like :
$str="297819 - 123C, WHITLEY ROAD";
that $str is got from adrress which is got from autocomplete jQuery, every address is defined into 2 part. first 297819 are "code", and 123C, WHITLEY ROAD is the "address".
i know hot to get the "code" with this following code :
$b = substr($str,0,strpos($str,"-")-1)
it will be return the "code" only.
guys can you help me how to get the address?
p.s every code and address will be disperated by -

Code and address separate by - so you can split it Using explode() as
$str="297819 - 123C, WHITLEY ROAD";
$data=explode("-",trim($str));// split string with -
echo $code=trim($data[0]);// get code
echo $address=trim($data[1]);// get address
And use trim() to remove unnecessary space

Related

How do I find the second, or third instance of a character in a string in PHP

I am trying to parse out the middle of the referrer information just to check from where the form submission is coming from.
Consider that the referrer is "https://www.somewebsite.com/someform.php
And what I want to extract is the part between those first two "//" and the next one "/".
I can get the string position, but how can I do a quick piece of code that will just parse out the part I need without several steps?
$ref=strpos($_SERVER['HTTP_REFERER'],'://');
gives me 5 (as the position where the "://" is found.
I want to use something like substr(string,start,length) the part out that is needed to get the pure referrer
the part that says "www.somewebsite.com"
without all the extra lines of code
used it
echo $_SERVER['HTTP_HOST'];

How to verfiy postcode using php?

I'm having postcode data in below format:
I need to map customer with their provided postcode.
EG. If customer enters postcode 36421 than it will assign to its
related representative that is "John".
If customer enter postcode 36222 than it will assign to related
representative that is "Sam".
If customer is from swiss country than it will assign to only one
representative as per data in image.
I'm confused how can I map customer.
I also tried with sub_str like,
$postCode = $_POST['postcode'];
$postCode3Digit = substr($postCode, 0, 3);
But it breaks.
Also tried with regex but at same moment i think than I have to write regex for every single postcode :(
I tried with switch case but it seems to be taking more time.
What will be the best way to achieve this ? Any help would be appreciated.
I would look it up by using regex, because ZIP-data often are a little too complex for substr() only. So what I would do is to loop trough the persons and to compare the zip of the person with the zip-input. Keep in mind that you need to replace the X (in your script) with dots. Dots are recognized as one single character in regex.
$search = '34567';
$reps = array('adam' => '12...',
'bdam' => '23...',
'cdam' => '345..',
'ddam' => '346..');
foreach($reps as $name => $zip){
if(preg_match('/^'.$zip.'$/', $search)){
echo $name; break;
}
}

PHP get 5 additional characters after a specified string of a particular page and list them

Note: I have edited the post on 2017/08/20
I'm trying to obtain a list of product page's URL that goes "www.example.com/product/11111/".
There are over 200 different products available and each of them has its own product page, I want to print out each product in a PDF file.
On "www.example.com/productlist/", there are URLs that lead to each product's page.
So, what I'm trying to do is
Obtain URLs that I need from "www.example.com/productlist/"
Generate PDF files of URLs that I have obtained
Insufficient Information: You did not provide me with much information about the code you already have and how the website will get the 200 URLs, so I can't write the whole code because it depends on the way your website will get the links from.
If you explain more about how the website is supposed to get the links, I will help you put them into an array and save them into a file and implement the rest of the code!
1-) Thing I understood is getting the last 5 characters
As you just want the last 5 characters, not the whole last part, you can do something like this.
$string = "http://example.com/folder/example/1234567"; //your link
$characters = strlen($string); //gets the characters count
$letters = 5; //edit 5 to show more or less manually
$code = substr($string, $characters - $letters, $characters);
echo $code; //will show the last 5 characters
I am always here to help. Good luck!
Start with parse_url().
$parts=parse_url("http://example.com/folder/example/12345");
That will give you an array with a handful of keys. The one you are looking for is path.
Split the path on / and take the last one.
$path_parts=explode('/', $parts['path']);
Your random numbers can now be stored like:
$number = $path_parts[count($path_parts)-1].

PHP extract String after pattern

I want to cut a html link with php.
The html link is everytime the same pattern
domain.com/forum/members/84564-name.html
I want to get the 84564 from the name.
the /forum/members/ is everytime the same.
and the "-" after the user-id is also everstime the same.
Can you help me to extract the user id?
I'm going to assume the user id may not always be 5 digits.
$domainSplit = explode("/", $theDomain); // split into parts
$theIdSplit=explode("-", $domainSplit[3]); // split 84564-name.html
$id=$theIdSplit[0];

Search for part of a string in array

i have been over so many posts online trying to sort this....
i am integrating a small search feature into a php script....
i have tried many scripts including in_array (only works for full match) and array_search
and nothing seems to work....
i have a script that creates an array with file names (all in the format "Random-Name-1.ext")
but the script removes the file extention, just leaving the filenames....
all filenames are seperated word by word with a -
all words have the first letter capital....
the array is called $files1;
the search string is called $search_string;
what im looking for would be along the lines of a foreach loop to check if the search string is contained in any part of each array value, and if it is, put the full array value into another array called $search_results
as the next part of my script to paginate needs the array $search_results to echo each of the filenames and display them 10 per page....
hope this is enough info, ive been workin on it for ages and racking my brains trying to find the correct code....
thanks in advance
..........
...........
EDIT.....
.......
........
got script working with preg_grep..... but i now have a slight problem....
the script before the search code is designed to get the url of the search page with q={search string}
and then trim this so that i just have the serach string as a variable.....
i use an str_replace to change the page url from
/games-search?q={search-string}
to {search-string}
this is perfectly fine, but the script to paginate the results as 10 per page adds ?page=2 to the url....
so when i click page 2, the str_replace to change the url to search string doesnt work now, as the new page url for page 2 is
/games-search?page=2&q={search-string}
i have been trying to do another str_replace to change the consecutive pages urls to just the search string but i am having problems with the regex to define the page number.....
the page numbers vary from 1 - about 50 (never more than 99, so 2 digits would be enough to match....
i have tried over and over again today to get this regex correct but i am not sure if i am going about it the correct way.....
here is my latest effort....
/games-search?page=(^[0-9][0-9]+)&
that is what i am trying to replace with "" (eg, nothing)
as i only need the data from after the & character in the url ... and thats IF the url even contains this (for exapmple the first page when the url doesnt contain the & character - if the url doesnt contain the & character , i dont want it modifying as i already have the data i need)
thanks again if anyone can help
decided not to paginate the results of each search as ther will never be more than about 15 results..... this is fine, but i am going to implement a miminum search length of 4 characters so it doesnt bring up 500 results for the letter A :)
Search an array for a substring match? preg_grep
Then do something like
$search_results = ...;
$paginated = array();
for ($i = 0; $i < count($search_results); $i++)
{
if ($i % 10 == 0) $paginated[$i + 1] = array();
$paginated[$i][] = $search_results[$i + 1];
}
Then print_r that array, and you'll see you've got something dead easy to work with.

Categories