I am trying to remove first 51 character of a long URL , I'm using
$sql = $db->Query(some query);
$mysite = $db->FetchArray($sql);
$str = $mysite['url'] ;
$str2 = substr('$str',51);
Above code return blank value, it works fine if i use plan text for $str
e.g.
$str = "I am looking for a way to pull the first 100 characters from a string"
$str2 = substr('$str',10);
my url is like "https://dl.dropbox.com/u/55299544/google.html?urlc=http://example.com"
i want to get http://example.com from database to show on user page,
how can i do this?
You're making this too complicated. If you are trying to get the http://example.com from the long URL then do this.
<?php
sql = $db->Query(some query);
$mysite = $db->FetchArray($sql);
$str = $mysite['url'] ;
$query = parse_url($str, PHP_URL_QUERY );
parse_str($query, $link);
echo $link["urlc"]; //http://example.com
?>
Related
I have a very long list of names and I am using preg_replace to match if a name from the list is anywhere in the string. If I test it with few names in the regex it works fine, but having in mind that I have over 5000 names it gives me the error "preg_replace(): Compilation failed: regular expression is too large".
Somehow I cannot figure out how to split the regex into pieces so it becomes smaller (if even possible).
The list with names is created dynamically from a database. Here is my code.
$query_gdpr_names = "select name FROM gdpr_names";
$result_gdpr_names = mysqli_query($connect, $query_gdpr_names);
while ($row_gdpr_names = mysqli_fetch_assoc($result_gdpr_names))
{
$AllNames .= '"/'.$row_gdpr_names['name'].'\b/ui",';
}
$AllNames = rtrim($AllNames, ',');
$AllNames = "[$AllNames]";
$search = preg_replace($AllNames, '****', $search);
The created $AllNames str looks like this (in the example 3 names only)
$AllNames = ["/Lola/ui", "/Monica\b/ui", "/Chris\b/ui"];
And the test string
$search = "I am Lola and my friend name is Chris";
Any help is very appreciated.
Since it appears that you can't easily handle the replacement from PHP using a single regex alternation, one alternative would be to just iterate each name in the result set one by one and make a replacement:
while ($row_gdpr_names = mysqli_fetch_assoc($result_gdpr_names)) {
$name = $row_gdpr_names['name'];
$regex = "/\b" . $name . "\b/ui";
$search = preg_replace($regex, '----', $search);
}
$search = preg_replace("/----/", '****', $search);
This is not the most efficient pattern for doing this. Perhaps there is some way you can limit your result set to avoid a too long single alternation.
Ok, I was debugging a lot. Even isolating everything else but this part of code
$search = "Lola and Chris";
$query_gdpr_names = "select * FROM gdpr_names";
$result_gdpr_names = mysqli_query($connect, $query_gdpr_names);
while ($row_gdpr_names = mysqli_fetch_assoc($result_gdpr_names)) {
$name = $row_gdpr_names['name'];
$regex = "/\b" . $name . "\b/ui";
$search = preg_replace($regex, '****', $search);
}
echo $search;
Still, print inside but not outside the loop.
The problem actually was in the database records. There was a slash in one of the records
How can I replace a certain part of a string. For example we have the URL:
username=[LINK]&quantity=10&limit=[POSTS]&interval=5&url=https://google.com/&service=762&runs=[RUNS]&type=Comments
I want to keep the parameters, quantity=\, limit=, interval=* & runs=*.
I've tried to do it with parsing but can't get it work,
parse_str($p_api, $query);
$quantity = '&quantity='.$query['quantity'];
$limit = '&limit='.$query['limit'];
$interval = '&interval='.$query['interval'];
$runs = '&runs='.$query['runs'];
How can I update all other data but keep these parameters as they are in the default string?
you can parse query parse_str(), update array value you wish and http_build_query() for url encoding
$str = "username=[LINK]&quantity=10&limit=[POSTS]&interval=5&url=https://google.com/&service=762&runs=[RUNS]&type=Comments";
parse_str($str,$query);
$query['username'] = "xyz";
$query['type'] = "post";
echo http_build_query($query);
Demo
I want to add a string between a url string using PHP.
$link = 'http://localhost/wordpress/mypage';
$string = 'nl/';
I want the new link to be like this:
$newlink = 'http://localhost/wordpress/nl/mypage';
Here is one-of-the way to achieve it, using substr_replace():
$someString = 'http://localhost/wordpress/mypage';
$string = 'nl/';
echo substr_replace($someString, $string, strpos($someString, 'mypage'), 0);
Output:
http://localhost/wordpress/nl/mypage
Another method using str_replace():
$someString = 'http://localhost/wordpress/mypage';
echo str_replace('wordpress/', 'wordpress/nl/', $someString);
this is an easiest way you can do.
$string = 'nl/';
$link = 'http://localhost/wordpress/'.$string.'mypage';
You can set the $string dynamic or static as you wanted to.
echo $link; Result : http://localhost/wordpress/nl/mypage
I have a strange problem with a simple preg_replace, if I use
$pattern = '/<div class="formula">(.*?)<\/div>/';
$str = preg_replace($pattern, "", $str);
not work correctly, nothing is replaced....if I put a static string instead of $str all work correctly.
string(2133) "Velocità: <div class="formulaTex">...</div><div class="formula">...</div>
why?there is some kind of encoding to use?
if I pass the var to preg_replace not work, if I pass the static string it work!
this is my code:
$db = new PDO('sqlite:ARGOMENTI_NEW.sqlite');;
$result = $db->query("SELECT * FROM Testi WHERE IDSezione = 100");
while($row = $result->fetchAll(PDO::FETCH_ASSOC)){
$pattern = '/<div class="formula">(.*?)<\/div>/';
$str = preg_replace($pattern, "", $row[0]["Testo"]);
echo $str . "<br/><br/><br/>";
}
thanks
I find the error, the string from sql query stamp encoded html entity, on the screen I see the correct string but real string have different characters....this is the problem! whit this I solve:
$str = preg_replace( "/\r|\n/", "", html_entity_decode($row[0]["Testo"]));
I am tryign to remove pagination page ID from URL. For example I have URL looks like this:
$urlVal = "http://192.168.1.233/sitename/property-list?page=13&page=11&sproperty=for sale&srooms=1,10&scity=&scountry=&lat=31.0000000&long=35.0000000&sprice=100,100000";
And want to remove, If any of matches match from URL:
1. page=1
2. page=1&page=2
3. page=1&page=2$page=3
4. page=1&page=2$page=3$page=4
In my current pagination code, Previous page is concating everytime when page is changed thats why I want to remove all page.
I have used this code but not working well.
$urlVal = "http://192.168.1.233/sitename/property-list?page=13&page=11&sproperty=for sale&srooms=1,10&scity=&scountry=&lat=31.0000000&long=35.0000000&sprice=100,100000";
//$getUrl =$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
$parts = parse_url($urlVal);
$urlVar = "";
$urlVar = $parts['query'];
echo $urlVar = preg_replace('/page=[0-9]&+/', '', $urlVar);
Try my code here: http://codepad.org/lSHV7a7w
I hope you understand what I am trying to do.
Thanks.
Try this.
$urlVal = "http://192.168.1.233/sitename/property-list?page=13&page=11&sproperty=for sale&srooms=1,10&scity=&scountry=&lat=31.0000000&long=35.0000000&sprice=100,100000";
$parts = parse_url($urlVal);
parse_str($parts['query'], $query);
unset($query['page']);
$newUrl = "{$parts['scheme']}://{$parts['host']}{$parts['path']}?" . http_build_query($query);
echo $newUrl;
// Regx Pattern
echo preg_replace('/&?page=[0-9]+&?/', '', $parts['query']);
How about using concatenation of numbers as page value?
$urlVal = "http://192.168.1.233/sitename/property-list?page=11,13&etc..."
It shortens url and makes removing parameter easy:
$urlVar = preg_replace('/page=[0-9,]+&/', '', $urlVar);
You can extract them from url using:
preg_match("/page=([0-9,]+)&/", $urlVal, $m);
$pages = explode(",", $m[1]);