How do you delete everything after first "space" in a string?

Lets say I have many random strings in the form of an URL:

http://www.mysite.com/ 20ALkJrhg88vtotAfU4H4ovV1yqaDictlziA
http://www.bobswebsite.com/ sadt244wesdfaftotAfU4H4ovV1yqaDiwet
http://www.google.com/ 235sagg88vtotAfU4H4ovV1yqaDicsdf

There is a space showing up here right after the domain name. How do I delete everything after this space and including the space using PHP?

Thanks

$str = explode(' ', $url);
$url = $str[0];

Thanks for the response, I’m sure it works normally, but it doesn’t seem to work in my case with an automatically generated web page in language translation. The software doesn’t seem to work with this.

preg_replace(‘~\s.*~’, ‘’, $url), where $url can be a single url or an array of urls.

Thanks stereofrog, but this still doesn’t seem to work for what I’m doing. The main thing I’m doing is using google to translate my webpage, but it produces strange URLs for links in my entire document when I translate my webpage from English to Spanish (es) for example:

http://66.102.9.104/translate_c?hl=en&sl=en&tl=es&u=[B]http://www.mysite.com/[/B]&usg=ALkJrhjlcqa4eDdFMivFuSoYH60Bj45_qQ

I used the following code below to get ride of a lot of the messy stuff:

			//modify all links
			if($translator == 'TRANSLATOR_GOOGLE'){
				$page = str_replace('href="http://64.233.179.104/translate_c?hl=en&sl=en&tl='.$lang.'&u=http://'.$site_url, 'href="http://'.$site_url.'/'.$lang, $page);
				$page = str_replace('href="http://64.233.179.104/translate_c?hl=en&sl=en&tl='.$lang.'&u=http://', 'href="http://', $page);
				
				$page = str_replace('%3F', '?', $page);
				$page = str_replace('%3D', '=', $page);
				$page = str_replace('%26', '&', $page);

This was able to reduce the links down to:

http://www.mysite.com/&usg=ALkJrhjlcqa4eDdFMivFuSoYH60Bj45_qQ

I tried to replace “&usg=” into a space to try to remove everything after it, since “&usg=” is common in all of these URLs. Unfortunately, the commands that you guys have given me make the document fail on the first “space” in the DOCTYPE at the very beginning of my page.

Please let me know if you can figure a way out to remove that last portion in the links.

Thanks

Simply replace “&usg” and the following stuff with nothing


$html = preg_replace('~&usg=\\w+~', '', $html);

Stereofrog, this almost did it! Most of the links are now showing up correctly, but not all of them. The links that show up now are similar to:

[B]http://www.mysite.com/[/B]-as56223aotW
[B]http://www.mysite.com/page1.php[/B]-xiWB2nQ
[B]http://www.othersites.com/single-page.php[/B]-B9
[B]http://www.differentsites.com/more-extra-pages.php[/B]-Adt4wfs553

As you can see, the bold URLs are the correct form and the thing that is now common in all of the stuff at the end is a dash “-” followed by random numbers and letters.

It seems now that I have to remove everything after (and including) the last dash, but I tried doing the command you gave me and it cuts it off after the first dash, so “single-page.php” would end up being just “single”.

Please let me know if there is a command to get rid of everything after that last dash.

Thanks

Lets try

$html = preg_replace(‘~&usg=[\w-]+~’, ‘’, $html);

Beautiful! Everything works perfectly now as far as I can see. I will let you know if I come across a problem in the document later on. I really appreciate your help Stereofrog.

Thanks
Best

Ah, you didn’t tell us the links contained an entity :wink: But I’m glad the problem has been solved.