Removing a space - php

I have a variable
$abc='akr:/9888/fk4f76mhn';
Another variable
$url = 'http://alpha.com/zidd/id/'.$abc;
Which is giving me
http://alpha.com/zidd/id/ akr:/9888/fk4f76mhn
A space is being introduced between id/ and akr:/9888/fk4f76mhn which I do not want.
Is there anything wrong I am doing here?

Well you should put quotes around your string literal
$abc = 'akr:/9888/fk4f76mhn';
If you still get a space, you can use trim to remove it:
$url = 'http://alpha.com/zidd/id/' . trim($abc);

I use: preg_replace( '/\s+/', '', $variable) to remove whitespace from inside a string, and trim, for the start and end. This may help you out.
The $variable would be the your $url.

try
$url = 'http://alpha.com/zidd/id/'.trim($abc);

Don't you want the following:
$abc='akr:/9888/fk4f76mhn';

Related

What is the correct way to str_replace until the end of string in PHP?

If I have a string like this:
myPDF12345431234
what would be the proper PHP function call to str_replace so that everything after " " rel=.... " is replaced with nothing/blank?
For further clarity, The value in between the link tags is dynamic and unknown, so I can't call it.
I'm looking for something like:
$oldstring = array('<a href="', '" rel="" *until the end_of_string* ');
$replacewith = array=('', '');
$newstring = str_replace($oldstring, $replacewith, $URL);
What is the proper way to get everything after the URL (quotation mark til the end of the string) to be replaced with nothing?
This is handled by regular expressions. The following replaces everything from rel="" to the end of the string.
$newstring = preg_replace('/ rel="".*$/','',$oldstring);
The .* means "everything" and $ means "end of string". I added a space before rel because I assume you want to drop that as well.
Use preg_match:
preg_match("/(\<a href\=\\".*\\")\srel/", "myPDF12345431234", $output_array);
Var_dump($output_array);
http://www.phpliveregex.com/p/fqQ
It sounds like what you're actually looking for is the substring of your current string up until the end of your rel attribute.
$newstring = substr($oldstring, 0, strpos($oldstring, 'rel=""'));

how change part of url by preg_replace?

I am trying to change all the links of a html with php preg_replace. All the uris have the following form
test.com/item/16
I want to change it to:
test.com/z/item/16
I tried the following, but it returns no changes:
$links = 'http://test.com/item/16';
preg_replace("/item","z/item",$links);
echo $links;
// output >>> http://test.com/z/item/16
You have to use delimiters as #nickb has pointed out, i.e. /your_regular_expression/. The / is the standard delimiter for regular expressions, and so, it being a special character, you'd have to escape the / you want to match by using a backslash, \/:
preg_replace("/\/item/","z/item",$links);
But luckily, you can choose your own delimiters, like #, so then no need to escape the /:
preg_replace("#/item#","z/item",$links);
Do this:
<?php
$links = 'http://test.com/item/16';
$a = preg_replace("/item/","z/item",$links);
echo $a;
preg_replace does not change the input string but instead returns a modified string....which is stored in $a variable..
You need delimiter and return of preg_replace set to variable
$links = 'http://test.com/item/16';
$links = preg_replace('/\/item/','/z/item',$links);
echo $links;
But, why don't you use just str_replace in this case?
The problem with the provided answers is that if you have more than one instance of /item in the URL, all of them will get replaced, for example a URL like:
http://items.domain.com/item/16
would get messed up, try modifying just the path:
$path = parse_url( $url, PHP_URL_PATH );
$url = str_replace( $path, '/z'.$path, $url );

Remove characters from a variable

I have the following in a variable, |MyString|
I want to strip the leading | and the ending | returning MyString
What is the quickest and non intensive way of doing this?
Easiest way is probably
$result = trim($input, '|');
http://docs.php.net/trim
e.g.
<?php
$in = '|MyString|';
$result = trim($in, '|');
echo $result;
prints MyString
Checkout the str_replace function in PHP http://php.net/manual/en/function.str-replace.php
this should remove all '|' characters:
str_replace('|','',$myString)
You may be able to use a regular expression to only remove the first and last '|' or alternatively using the String trim() function may also work:
http://www.php.net/manual/en/function.trim.php
So, something like this:
$trimmedMyString = trim($myString, "|");
Worth trying anyway.

Strip " from outside $var

$varHi I know this is an extremely basic task, but I am some what confused.
I am pulling a String back from a Database and assigning it to $var. I am then outputting this value into a text area. However, when I do, the string is surrounded in " ".
e.g. "This is the String", but I just want : This is the String
I have tried many functions. I am using chr(34) to search for the ", but to no avail. It will only replace them if it is inside the string. Not on the outside / surrounding the string.
$var = str_replace( chr(34), "" ,$var);
Thanks In Advance for any help.
EDIT : Turn's out I was outputting incorrectly into the text area
""
should have been
Thank's for the help.
$var = str_replace( '"', '' ,$var);
See it in action here
$var = str_replace('"', '', $var);
What about $var = str_replace('"', '', $var);?
you could use str_replace, as already mentioned but that would remove quotes from the string body also (if you have any)
to remove only the first and last ones you could use the trim function with the optional second parameter
edit: and if you have quotes inside the string that you want to keep those might be escaped so you might use str_replace to use only the quotes instead the escaped quotes ( str_replace('\"', '"', $string) );
The double speech should only appear if they are in your data being pulled, unless you are echoing or printing to the text area incorectly.
As said above the
$var = str_replace('"', '', $var);
Will work fine, but its a bit of a hack if your data doesn't have the double speech in it to start with.

Preg_match, Replace and back to string

sorry but i cant solve my problem, you know , Im a noob.
I need to find something in string with preg_match.. then replace it with new word using preg_replace, that's ok, but I don't understand how to put replaced word back to that string.
This is what I got
$text ='zda i "zda"';
preg_match('/"(\w*)"/', $text);
$najit = '/zda/';
$nahradit = 'zda';
$o = '/zda/';
$a = 'if';
$ahoj = preg_replace($najit, $nahradit, $match[1]);
Please, can you help me once again?
You can use e.g. the following code utilizing negative lookarounds to accomplish what you want:
$newtext = preg_replace('/(?<!")zda|zda(?!")/', 'if', $text)
It will replace any occurence of zda which is not enclosed in quotes on both sides (i.e. in U"Vzda"W the zda will be replaced because it is not enclosed directly into quotes).

Categories