PHP stripping strings - php

I would like to strip everything in a string before a - and leave what's after that. For example:
15.11-101
I want to strip 15.11 and leave 101. I've tried a few different things, but can't seem to get it working, if anyone could help me out, that'd be fantastic :)
Cheers
Leanne

You could use something like this:
$result = substr($original, strpos($original, '-') + 1);

assuming it appears only once, you can use the explode function.
$s='15.11-101';
$a=explode('-',$s,1);
echo $a[1];
This should be the fastest way.

Related

How to extract string from another string between the Nth and N+1th 'breakword' in php?

My example code is given below.
$a= "I think there are many people who can help in my difficulty. breakword Thank you in advance. brwakword If i got this solution I will be happy. Breakword I have already searched in google about it I did not get it's solution. Breakword If you have any link of solution please help me. breakword Thank you again!";
From the above string I want to get the sentence between 2nd and 3rd 'breakword' word.
It means I want to extract 'If i got this solution I will be happy.'.
If you have any solution please help me. Thank you in advance
You can split your string into smaller parts using explode() and get the "Nth" part of the array.
$sentences = explode('breakword', $a);
echo $sentences[2];
PHP > 5.4 will support this too :
echo explode('breakword', $a)[2];
a similar approach but intead of using explode you can also use preg_split
supports (PHP 4, PHP 5)
preg_split('/breakword/', $a);
for the second answer you can use
$dummy=array();
preg_match_all('/breakword/', $a,$dummy)

How to use regex in PHP to get int after slash in string

I've tried to create a code in PHP that gets the topic id after the forward slash in a given string. However the problem i'm having is that its returning nothing, how can i make it return the int?
echo preg_match('/([^/]+)/', 'Learning-English/478', $discussion_id);
echo $discussion_id;
This is for an online forum, thank you for your help; it's much appreciated. If you need ay more information please don't hesitate to leave a comment.
preg_match("#/(\d+)$#", 'Learning-English/478', $discussion_id);
That would work for you.
To print the id(matched number); you need to echo $discussion_id[1].
Here is a working link.
For the newer strings, you wouldn't be needing the string end match($). Thus, the regex will be:
preg_match("#/(\d+)#", 'Looking-for-Pen-Pals/1161&t=viewDiscussion', $discussion_id);
echo $discussion_id[1];
you could just use explode() like this:
$arr = explode('/',$string);
$discussion_id = $arr[count($arr)-1];
this will split your string and then get you the last part.

Change url variable using php

Say I have a url like this in a php variable:
$url = "http://mywebsite.extension/names/level/etc/page/x";
how would I automatically remove everything after the .com (or other extension) and before /page/2?
Basically I would like every url that could be in $url to become http://mywebsite.extension/page/x
Is there a way to do this in php? :s
thanks for your help guys!
I think parse_url() is the function you're looking for. You can use it to break down an URL into it's component parts, and then put it back together however you want, adding in your own things as needed.
As PeeHaa noted, explode() will be useful for dividing up the path.

PHP string contains $ and incorrectly recognizes variable

Basically I'm sending a formatted string to a PHP script via POST
So say I have the string... "abcdef$ghikl" it will recognize $ghikl as a variable...
Is there any ways to tell PHP that no variables exist within this string?
I know how to hard code it, you just use "'", but since the string is being sent to the script, I don't know what to do...
Thanks guys
For the scope of the question, all the code that's really needed is this:
$string = $_POST['string'];
// a couple strcmp's here, just to see if the string == ""
$array = explode("+", $string);
Try escaping the $
"abcdef\$ghikl"
PHP won't see that as a variable unless you eval() it. Can you show us your code?

How to rid of extra white space?

I have a textbox that i use nl2br to insert into db. The problem is that is adding too much space. Is there a way to assign this an id while being processed or some other way to reduce the space between sentences?
Looks like what you want is some cleanups of those lines.
This is what I use on my php apps.
<?php
function trimlines($html){
$ret = str_replace("\r\n","\n",$html);
$ret = explode("\n",$ret);
$ret = array_map('trim',$ret);
$ret = join("\r\n",$ret);
return $ret;
}
?>
Regular expressions can also be used, but for quick cleanups, this works well for me.
Hope it helps :)
I would suggest wrapping the variable in sql with TRIM('sentences here')

Categories