PHP - How can I replace dashes with spaces? - php

I am currently using the following code to convert my strings to seo friendly urls:
function url($url) {
$url = str_replace(" ", " ", $url);
$url = str_replace(array("'", "-"), "", $url);
$url = mb_convert_case($url, MB_CASE_LOWER, "UTF-8");
$url = preg_replace("#[^a-zA-Z]+#", "-", $url);
$url = preg_replace("#(-){2,}#", "$1", $url);
$url = trim($url, "-");
return $url;
}
When I query my database I match the url against the article titles in my database, my problem is that after performing the seo friendly url function the urls do not match any article titles in my database.
The addition of dashes (not sure about the lowercase) means that they are completely different to the entries in the database.
What is my next step, should I remove the dashes before querying the database, if so how?
Or is it better practice to include the article id in my url somewhere and reference it?

Querying by id seems far faster and simplier to me than reconverting back your titles, using url rerwriting to ignore the title (just for referencement) and call a page with the id as a GET argument. Looking at the current URL let me think that StackOverflow works this way.
Using the current page as an example, i suspect that
http://stackoverflow.com/questions/8034788/php-how-can-i-replace-dashes-with-spaces
is rewritten to something like
http://stackoverflow.com/questions.php?id=8034788
where a simple SQL query gets the content of the article.

Related

Regex to filter int from url

I'm trying to filter out a value of a url.
The url looks like the following:
http://userimages-akm.imvu.com/catalog/includes/modules/phpbb2/images/avatars/145870556_47076915459092eafd7b69.jpg
Now i'm trying to only receive the following part from the url: 145870556
I thought about using a regex. But i won't get a working regex beside this one:
^((http[s]?|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?$
Is there a better regex to use?
If the image filename always follows the same format <timestamp>_<hex-value>.<extension>, then you don't need to match the entire URL.
$url = 'http://userimages-akm.imvu.com/catalog/includes/modules/phpbb2/images/avatars/145870556_47076915459092eafd7b69.jpg';
preg_match_all('~\/(\d+)_.*$~', $url, $matches);
// $matches[1] = '145870556';
https://regex101.com/r/vsDnoj/2

Preserve white space in URL with PHP

I am stuck big time on this problem and google has been of no help to me so far. I am trying to find a way to preserve white space in a URL with moderate to no luck.
I have a form that needs to gather post data, mail it, and then append the post data to the URL as comma separated value and redirects them to a page where they download a product.
Once the user presses download that page reads the data in the URL and applies it to a billing invoice (the program is billed on time usage).
A simplified example:
$addressOne = $_POST['addressOne'];
$newURL = "http://subdomain.domain.com/connectnow=on?" . ", Address1=" . $addressOne;
If(mailSent) {
header("Location: $newURL")
}
There are a lot more values obviously, but the address is one of the areas that I am having this issue.
I have tried doing something like:
$newURL = str_replace(" ", " ", $newURL);
That worked as far as preserving the whitespace in the URL visually, but when the program that gets downloaded reads the URL it replaces the as %C2%.
I have also tried:
$newURL = str_replace(" ", " \40", $newURL);
That made the spaces in the URL convert back to %20.
Any guidance would be appreciated.
URL:
www.site.com/my spaces preserved/
urlencode()
www.site.com%2Fmy+spaces+preserved%2F
urldecode()
www.site.com/my spaces preserved/

Which characters in urls cause file_get_contents / curl to fail?

EDIT FOR CLARIFICATION:
I would like to know which characters in a url cause file_get_contents / curl to fail.
In the example below, the only character which causes a problem is the space, so the best thing for me to do would simply be to str_replace spaces in the url with %20. Are there any other characters which also cause it to fail? If so, what are they? Is there a function which does this replacement for me?
ORIGINAL PHRASING:
I'd like to be able to download an arbitrary file by its URL, chosen by the user, and have access to it as a string. My initial reaction was:
$str = file_get_contents($url);
However, this fails on URLs like:
http://i.ebayimg.com/t/2-WAY-PHOTO-FRAME-KEY-BOX-SHABBY-CHIC-STYLE-/00/s/NjAwWDYwMA==/$(KGrHqRHJDoE-PBe-SSLBPlrnIYb Q~~60_35.JPG
Next, I tried cURL:
function file_get_contents_curl($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
However, for the same URL, cURL fails with "Invalid URL".
I've read on a number of questions here that when downloading from URLs with arbitrary characters in them, urlencode must be used. However, this results in:
http%3A%2F%2Fi.ebayimg.com%2Ft%2F2-WAY-PHOTO-FRAME-KEY-BOX-SHABBY-CHIC-STYLE-%2F00%2Fs%2FNjAwWDYwMA%3D%3D%2F%24%28KGrHqRHJDoE-PBe-SSLBPlrnIYb+Q%7E%7E60_35.JPG
which doesn't fetch either, using either method, I think because now it thinks it's a local file. What do I need to do to be able to fetch an arbitrary url?
Try this:
$url = "http://i.ebayimg.com/t/2-WAY-PHOTO-FRAME-KEY-BOX-SHABBY-CHIC-STYLE-/00/s/NjAwWDYwMA==/$(" . urlencode("KGrHqRHJDoE-PBe-SSLBPlrnIYb Q~~60_35.JPG");
$str = file_get_contents($url);
Edit: As Galen said the only problem with URL is the space and it can be fixed using str_replace as below.
$url = "http://i.ebayimg.com/t/2-WAY-PHOTO-FRAME-KEY-BOX-SHABBY-CHIC-STYLE-/00/s/NjAwWDYwMA==/$(KGrHqRHJDoE-PBe-SSLBPlrnIYb Q~~60_35.JPG";
$url = str_replace(' ', '+', $url);
$str = file_get_contents($url);

Using $_SERVER['HTTP_REFERER'];, but modify for www

I'm using some code like this to grab the URL from an inbound link:
$inbound_url = $_SERVER['HTTP_REFERER'];
//then do some stuff writing the url to a database table, but....
//ONLY IF the url doesn't already exist in the table
Let's say the link comes in from the same website, same webpage, but different only in the www. So I get this:
1) http://www.mysite.com/page.html
2) http://mysite.com/page.html
This shows up twice in my table since one has the www and one doesn't.
Is there a way to parse the results of $_SERVER['HTTP_REFERER']; to either:
1) add www. where it's missing, OR
2) strip everything of ...http://...www. or ..http://
Thanks in advance as always.
Sure you can. Some simple string manipulation and replacement should be all you need to remove the www from any URL -
$inbound_url = str_replace('http://www','http://',$inbound_url);
As defined in the documentation -
str_replace() - Replace all occurrences of the search string with the replacement string
Notice that I'm including the http:// in the search so that any other occurrence of the string www URL will remain untouched.
Use this
$url = 'http://stackoverflow.com';
$d = array_shift( explode( '.', str_replace('www.', '', parse_url( $url, PHP_URL_HOST )) ) );
echo $d; //stackoverflow
or you can also use
http://php.net/manual/en/function.parse-url.php function

PHP: Change only a portion of a URL string?

I’m working on a small hoppy project where I want to replace a specific page on a URL. Let me explain:
I’ve got the URL
http://www.example.com/article/paragraph/low/
I want to keep the URL but replace the last segment /low/ with /high/ so the new URL is:
http://www.example.com/article/paragraph/high/
I’ve tried different explode, split and splice but I just can’t seem to wrap my head around it and make it work. I can change the entire URL but not just the last segment and save it in a new variable.
I’m pretty confidence that it is a pretty straight forward case but I’ve never worked that much with arrays / string-manipulation in PHP so I’m pretty lost.
I guess that I have to first split the URL up in segments, using the "\" to separate it (I tried that but have problems by using explode("\", $string)) and then replace the last \low\ with \high\
Hope someone could help or point me in the right direction to what methods to use for doing this.
Sincere
Mestika
how about str_replace?
<?php
$newurl = str_replace('low', 'high', $oldurl);
?>
documentation;
http://php.net/manual/en/function.str-replace.php
edit;
Rik is right; if your domain (or any other part of the url for that matter) includes the string "low", this will mess up your link.
So: if your url may contain multiple 'low' 's, you will have to add an extra indicator in the script. An example of that would be including the /'s in your str_replace.
You took \ for /.
$url = explode('/', rtrim($url, '/'));
if (end($url) == 'low') {
$url[count($url)-1] = 'high';
}
$url = implode('/', $url) .'/';
Use parse_url to split the URL into its components, modify them as required (here you can use explode to split the path into its segments), and then rebuild the URL with http_build_url.
<?php
class TestURL extends PHPUnit_Framework_TestCase {
public function testURL() {
$URL = 'http://www.mydomain.com/article/paragraph/low/';
$explode = explode('/', $URL);
$explode[5] = 'high';
$expected = 'http://www.mydomain.com/article/paragraph/high/';
$actual = implode('/', $explode);
$this->assertEquals($expected, $actual);
}
}
--
phpunit simple-test.php
PHPUnit 3.4.13 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 4.75Mb
OK (1 test, 1 assertion)
This will probably be enough:
$url = "http://www.mydomain.com/article/paragraph/low/";
$newUrl = str_replace('/low/', '/high/', $url);
or with regular expressions (it allows more flexibility)
$url = "http://www.mydomain.com/article/paragraph/low/";
$newUrl = preg_replace('/low(\/?)$/', 'high$1', $url);
Note that the string approach will replace any low segment and only if it's followed by a /. The regex approach will replace low only if it's the last segment and it may not be followed by a /.

Categories