更有效地运行3 str_replace(Run 3 str_replace more efficiently)
所以我目前运行以下代码:
$current_link = get_author_posts_url($user_id,strtolower($user_info->user_login)); $current_link = str_replace(" ", "-", $current_link); $current_link = str_replace(".-", "-", $current_link); $current_link = str_replace("author", "authors", $current_link);
但是我觉得这段代码可能更有效率。 因为我在同一个字符串上运行str_replace 3次。 所以我使用
preg_replace
来最小化代码,如下所示:$cLPatterns = array(' ', '.-'); $current_link = preg_replace($cLPatterns, '-', $current_link); $current_link = str_replace("author", "authors", $current_link);
但有没有办法使用
str_replace("author", "authors", $current_link)
作为preg_replace
一部分如何使此代码最有效。
干杯
So I currently run the following code:
$current_link = get_author_posts_url($user_id,strtolower($user_info->user_login)); $current_link = str_replace(" ", "-", $current_link); $current_link = str_replace(".-", "-", $current_link); $current_link = str_replace("author", "authors", $current_link);
However I feel that this code could be more efficient. As I'm running str_replace 3 times on the same string. So I used
preg_replace
to minimize the code like so:$cLPatterns = array(' ', '.-'); $current_link = preg_replace($cLPatterns, '-', $current_link); $current_link = str_replace("author", "authors", $current_link);
But is there a way to use the
str_replace("author", "authors", $current_link)
as part ofpreg_replace
How can I make this code the most efficient.
Cheers
原文:https://stackoverflow.com/questions/44533491
最满意答案
听起来你正在寻找分页。 这与你的服务器有关,而不是你的客户端。
实现分页的典型方法是让服务器接受偏移量并限制集合资源的参数。
例如,假设你的服务器上有一些你可以检索的项目
GET myapi/items
您可以引入偏移量和限制查询参数,以指定您要收集哪个页面。
GET myapi/items?offset=50&limit=25
然后,您将相应地编程您的API客户端以使用这些值。
你可以在服务器上推出你自己的分页机制,或者你可以使用符合你所使用的语言和/或框架的东西,这可能是更好的方式去做这件事(最好不要重新发明轮子) 。 例如,我使用Spring,所以使用spring-data的分页功能。
这段视频对于从53:00开始的分页有相当好的解释
It sounds like you're looking for pagination. This has more to do with your server than your client.
A typical way to implement pagination, is to have your server accept offset and limit parameters for a collection resource.
So for example, say you had a collection of items on your server that you could retrieve with
GET myapi/items
You would introduce the offset and limit query parameters to specify which page you wanted out of your collection.
GET myapi/items?offset=50&limit=25
Then you would program your API client accordingly to make use of the values.
You can roll your own paging mechanism on the server, or you can use something that fits with the language and / or framework you're using, which is probably a better way to go about this (best not to re-invent the wheel). For example, I'm using Spring, so use the paging features of spring-data.
This video has a fairly good explanation of paging starting at 53:00
相关问答
更多-
如何在Android RxJava Observable中按顺序运行2个查询?(How to run 2 queries sequentially in a Android RxJava Observable?)[2023-08-04]
运营商要做到这一点将是merge ,请参阅http://reactivex.io/documentation/operators/merge.html 。 我的方法是创建两个observable,让我们说observableLocal和observableRemote ,并合并输出: Observable -
按顺序下载文件(Download files sequentially)[2021-12-13]
FileOutputStream output = new FileOutputStream(file); file = new File(filepath + "/" + i+".txt"); file在第一次迭代中为空,并且由于异常而保持为空。 您应该在创建输出流之前初始化file变量。 FileOutputStream output = new FileOutputStream(file); file = new File(filepath + "/" + i+".txt"); file is n ... -
如何有效地从Android中的Firebase下载数据?(How to download data from Firebase in Android efficiently?)[2022-07-22]
Firebase对其所有(网络,磁盘等)操作使用单个单独的线程。 但是您的回调将始终在主线程上调用,以便您可以安全地与UI进行交互。 但是如果你在回调中执行任何非平凡的操作,那么(通常)你的工作是在主线程上执行这些操作。 所以AsyncTask或IntentService仍然是那里的正确方法。 Firebase uses a single separate thread for all its (network, disk, etc) operations. But your callbacks will ... -
很简单: 1)创建一个数据库并插入你的网址。 2)在您的清单中为downloadmanager下载完整操作创建一个接收器。 3)当收到下载完成后,从数据库读取一行并开始新的下载(入队)。 快乐编码:-) I found the answer! The problem with IntentService is it execute DownloadManager.engueue() so fast and as a result all download get downloaded together. S ...
-
如果您的数据“非常便宜”,那么我建议您将其保留在应用程序中。 由于您要在离线模式下运行应用程序,因此不必依赖Internet。 If your data 'is very cheap in size' as you are saying, then I would suggest you to keep it within the application. Keeping dependency on internet is unnecessary as you are saying you want to ...
-
您可以在新服务器上使用wget从旧服务器下载数据。 就像是 $ wget -r -N -l inf 'ftp://old.example.com/' 应该这样做。 如果您需要更具体的内容,请查阅手册页。 wget非常强大。 或者,可以直接使用FTP从服务器传输到服务器。 由于FTP使用单独的控制和数据连接,因此可以将数据连接建立到与控制连接源自的主机不同的主机。 这是通过将其中一台服务器切换到被动模式来完成的。 我认为SecureFX通过打开两台服务器的连接并在它们之间拖放文件来支持这种操作模式。 但是, ...
-
此代码将从url下载任何文件,只需替换url和位置.. public class AndroidDownloadFileByProgressBarActivity extends Activity { // button to show progress dialog Button btnShowProgress // Progress Dialog private ProgressDialog pDialog; // Progress dialog type ...
-
你可以从这里下载https://dl.dropbox.com/s/9s65mxj67ix4t3d/Vitamio-SDK.7z?dl=1 you can download it from here https://dl.dropbox.com/s/9s65mxj67ix4t3d/Vitamio-SDK.7z?dl=1
-
听起来你正在寻找分页。 这与你的服务器有关,而不是你的客户端。 实现分页的典型方法是让服务器接受偏移量并限制集合资源的参数。 例如,假设你的服务器上有一些你可以检索的项目 GET myapi/items 您可以引入偏移量和限制查询参数,以指定您要收集哪个页面。 GET myapi/items?offset=50&limit=25 然后,您将相应地编程您的API客户端以使用这些值。 你可以在服务器上推出你自己的分页机制,或者你可以使用符合你所使用的语言和/或框架的东西,这可能是更好的方式去做这件事(最好不要重 ...
-
isolveit,因为我的cygwin没有下载git,所以我在cygwin下载,一切都好 isolveit,because my cygwin not download git,so i download in the cygwin,all is ok