How to echo explode array? - php

EDIT: More clear: I want to echo $data as an array.
I have this:
$lic = $_GET['lic'];
$locationtodb = '../../files/';
$licenses = $locationtodb.'filea.txt';
$SearchLicense = $lic;
$pattern = preg_quote($SearchLicense, '/');
$pattern = "/^.*$pattern.*\$/m";
preg_match_all($pattern, $licenses, $matches);
$wholeLine = implode("\n", $matches[0]);
$data = explode(":", $wholeLine);
I can use somethings like this to replace strings:
$CurrentLine = $lic.':'.'active'.':'.$data[2];
$NewLine = $lic.':'.'suspended'.':'.$data[2];
$new_contents = file_get_contents($licenses);
$new_contents = str_replace($CurrentLine,$NewLine,$new_contents);
file_put_contents($licenses,$new_contents);
And it does work !
But if I typed something like:
echo $data[2];
It give nothing ...
Why?
Thanks.

Solution discovered!
$licenses = $locationtodb.'filea.txt';
Must be:
$licenses = file_get_contents($locationtodb.'filea.txt');

Related

PHP trim and keep only first and last part

I have variables as follow in PHP
steve_s_baue
marine_camp_se_bell
mike_wane
I want to only keep the first and last part:
steve_baue
marine_bell
mike_wane
I tried to use trim but got stuck.
EDIT: Here is what I tried to far
$row = $pre_results[$i];
$name = $row -> name;
$text = preg_replace('~[^\pL\d]+~u', '_', $name);
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
$text = preg_replace('~[^-\w]+~', '', $text);
$text = trim($text, '_');
$text = preg_replace('~-+~', '_', $text);
$text = strtolower($text);
Any suggestions?
function firstlast($var,$seperator) {
$varr = explode($seperator, $var);
$first = current($varr);
$last = end($varr);
return $first.'_'.$last;
}
$seperator='_';
$old = 'marine_camp_se_bell';
$new = firstlast($old, $seperator);
echo $new;
If you put original strings in an array, you can run array_walk(); with this function
You can check this:
http://nimb.ws/dVHzZJ and
http://nimb.ws/ENgTCm
<?php
$a = "steve_s_baue";
$temp=explode("_",$a);
$arr=array($temp[0],$temp[2]);
print_r(implode("_",$arr));
?>
Do it with explode function, I made a basic example working
<?php
$name = "marine_camp_se_bell";
$var = explode("_",$name);
print_r (explode("_",$name));
//than print as array
echo "</br>";
echo $var[0];
echo "</br>";
echo $var[3];
?>
$name = "john_dont_do_some_doe";
$array = explode("_", $name);
$last = count($array) - 1;
echo $array[0]."_". $array[$last]; //shows: john_doe
*not tested, but should do the job

php search on string comma separated and get element that match

I have a question, if anyone can help me to solve this. I have a string separated by commas, and I want to find an item that partially matches:
$search = "PrintOrder";
$string = "IDperson, Inscription, GenomaPrintOrder, GenomaPrintView";
I need to get only the full string from partial match as a result of filter:
$result = "GenomaPrintOrder";
With preg_match_all you can do like this.
Php Code
<?php
$subject = "IDperson, Inscription, GenomaPrintOrder, GenomaPrintView, NewPrintOrder";
$pattern = '/\b([^,]*PrintOrder[^,]*)\b/';
preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
foreach ($matches as $val) {
echo "Matched: " . $val[1]. "\n";
}
?>
Output
Matched: GenomaPrintOrder
Matched: NewPrintOrder
Ideone Demo
$search = "PrintOrder";
$string = "IDperson, Inscription, GenomaPrintOrder, GenomaPrintView";
$result = array();
$tmp = explode(",", $string);
foreach($tmp as $entrie){
if(strpos($entrie, $string) !== false)
$result[] = trim($entrie);
}
This will get you an array with all strings that match your search-string.
You can use regular expression to get the result:
$search = "PrintOrder";
$string = "IDperson, Inscription, GenomaPrintOrder, GenomaPrintView";
$regex = '/([^,]*' . preg_quote($search, '/') . '[^,]*)/';
preg_match($regex, $string, $match);
$result = trim($match[1]); // $result == 'GenomaPrintOrder'
$search = "PrintOrder";
$string = "IDperson, Inscription, GenomaPrintOrder, GenomaPrintView";
$array = explode(" ", $string);
echo array_filter($array, function($var) use ($search) { return preg_match("/\b$searchword\b/i", $var); });
Since there are so many different answers already, here is another:
$result = preg_grep("/$search/", explode(", ", $string));
print_r($result);

URL Rewrite - change URL text between slashes

I have URLs like this:
http://www.mywebsite.com/carmake/ABCDEFG/123456789
http://www.mywebsite.com/carmake/AAABBBC/124532532
http://www.mywebsite.com/carmake/BNDFKNV/463634213
and I want to change them to this:
http://www.mywebsite.com/carmake/parts/123456789
http://www.mywebsite.com/carmake/parts/124532532
http://www.mywebsite.com/carmake/parts/463634213
How can I change the text between the last to slashes to parts in functions.php
https://regex101.com/r/sH1wA1/1
<?php
$string = 'http://www.mywebsite.com/carmake/ABCDEFG/123456789';
$pattern = '/(.*\/).*\/([^\/]*$)/';
$replacement = '${1}parts/${2}';
echo preg_replace($pattern, $replacement, $string);
?>
Try this:
<?php
$url = "http://www.mywebsite.com/carmake/ABCDEFG/123456789";
$parts = parse_url($url);
$path = $parts['path'];
$pos = strpos($path, '/', 9);
$sub = substr($path, 9, $pos - 9);
$url = str_replace($sub, 'parts', $url);
Split to segments, change and collect back
$a = 'http://www.mywebsite.com/carmake/BNDFKNV/463634213';
$to = 'parts';
$s = explode('/', $a);
$s[count($s)-2] = $to;
echo implode('/', $s);

Unexpected result with preg_replace()

$subject = "'foo' = 'bar'";
$pattern = "/('foo' = ').*(')/";
$var = "123baz";
$replacement = "$1$var$2";
print_r(preg_replace($pattern, $replacement, $subject));
Result is 23baz' instead of 'foo' = '123baz'. Why and how can I fix this?
Think you're looking for something like this:
$subject = "'foo' = 'bar'";
$pattern = "/(= ').*(')/";
$var = "123baz";
$replacement = "= '$var'";
print_r(preg_replace($pattern, $replacement, $subject));
Output:
'foo' = '123baz'

echo end of url without backslash

http://www.mywebsite/product-tag/animal/
How to I echo animal without the backslash and in big caps like this:
ANIMAL
$link = 'http://www.mywebsite/product-tag/animal/';
$parts = explode( '/', $link );
echo(strtoupper($parts[4]));
If you want auto searching, then you need to use preg_match.
<?php
$string = 'http://www.mywebsite/product-tag/animal/';
$urlparts = explode("/", $string);
$animal = ($string[strlen($string)-1] == '/'? $urlparts[count($urlparts)-2] : end($urlparts));
echo strtoupper($animal);
?>
actually I just figured it out
$r = $_SERVER['REQUEST_URI'];
$r = explode('/', $r);
$r = array_filter($r);
$r = array_merge($r, array());
$endofurl = $r[1];
echo $endofurl;

Categories